iOS-NSAttributedString属性关键字

2024-08-21 17:48

本文主要是介绍iOS-NSAttributedString属性关键字,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.获取系统字体

var dataArray:[String] = UIFont.familyNames()
中文字体没效果


fontName.jpg

2.关键字

NSAttributedString中的关键字:

keyvalue说明
NSFontAttributeNameUIFont字体名称,大小
NSParagraphStyleAttributeNameNSParagraphStyle段落样式
NSForegroundColorAttributeNameUIColor前景色
NSBackgroundColorAttributeNameUIColor背景色
NSLigatureAttributeNameNSNumber(integer)连体
NSKernAttributeNameNSNumber(float)字符间距
NSStrikethroughStyleAttributeNameNSNumber(integer)删除线
NSStrikethroughColorAttributeNameUIColor删除线颜色
NSUnderlineStyleAttributeNameNSNumber(integer)下划线
NSStrokeColorAttributeNameUIColor描边颜色
NSStrokeWidthAttributeNameNSNumber(float)描边宽度
NSShadowAttributeNameNSShadow阴影
NSTextEffectAttributeNameNSString特殊效果
NSAttachmentAttributeNameNSTextAttachment用于图文混排
NSLinkAttributeNameNSURL链接
NSBaselineOffsetAttributeNameNSNumber(float)上下偏移
NSObliquenessAttributeNameNSNumber(float)倾斜
NSExpansionAttributeNameNSNumber(float)拉伸
NSWritingDirectionAttributeNameNSArray of NSNumbers顺序
NSVerticalGlyphFormAttributeNameNSNumber排版

3.例子

UILabel,UITextField,UITextViewattributedText属性,这里使用UITextView来举例
当UITextView添加到UINavigationView时, UITextView顶部会有一段64px偏移,继承于UIScrollView是控件都有这个偏移,需要设置:

self.automaticallyAdjustsScrollViewInsets = false

或者在storyboard中,去掉属性勾选


font18.png
 @IBOutlet weak var fontTextView: UITextView!
1.字体名称大小,前景,背景
NSFontAttributeName
NSForegroundColorAttributeName
NSBackgroundColorAttributeName
let originString = "NSAttributedString 文本属性关键字"
let font = UIFont(name: "Marker Felt", size: 17)!
let attr = [NSFontAttributeName:font, NSForegroundColorAttributeName:UIColor.redColor(), NSBackgroundColorAttributeName:UIColor.yellowColor()]
let attrString = NSAttributedString(string: originString, attributes: attr)

font01.png
2.字符间距
NSKernAttributeName
let originString = "NSAttributedString 文本属性关键"
let attr = [NSKernAttributeName:5]
let attrString = NSAttributedString(string: originString, attributes: attire)

font02.png
3.删除线,下划线
删除线
NSStrikethroughStyleAttributeName
NSStrikethroughColorAttributeName
let originString = "NSAttributedString 文本属性关键字"
let attr = [NSStrikethroughStyleAttributeName:1, NSStrikethroughColorAttributeName:UIColor.purpleColor()]
let attrString = NSAttributedString(string: originString, attributes: attr)

NSStrikethroughStyleAttributeName的value:
1~7单线,依次加粗
9~15:双线,依次加粗


font03.png

font04.png

font05.png
下划线
NSUnderlineStyleAttributeName
NSUnderlineColorAttributeName

1~7单线,依次加粗
9~15:双线,依次加粗

let originString = "NSAttributedString 文本属性关键字"
let attr = [NSUnderlineStyleAttributeName:1, NSUnderlineColorAttributeName:UIColor.purpleColor()]
let attrString = NSAttributedString(string: originString, attributes: attr)

font06.png
4.阴影
NSShadowAttributeName
let originString = "NSAttributedString 文本属性关键字"
let shadow = NSShadow()
shadow.shadowColor = UIColor.purpleColor()
shadow.shadowOffset = CGSizeMake(5, 5)
shadow.shadowBlurRadius = 2.0
let attr = [NSShadowAttributeName:shadow, NSFontAttributeName: UIFont.systemFontOfSize(20)]
let attrString = NSAttributedString(string: originString, attributes: attr)
fontTextView.attributedText = attrString;

font07.png
5.链接
NSLinkAttributeName
var originString = "www.google.cn"
let attr = [NSLinkAttributeName: NSURL(string: "http://www.google.cn", NSFontAttributeName: UIFont.systemFontOfSize(15)]
let attrString = NSAttributedString(string: originString, attributes: attr)
fontTextView.attributedText = attrString;

font09.png


此时链接还不可以点击,在Storyboard中设置fontTextView属性:


font08.png
6.上下偏移
NSBaselineOffsetAttributeName
let originString = "String文本  "
let attrString = NSMutableAttributedString()
let attr01 = [NSBaselineOffsetAttributeName:5, NSUnderlineStyleAttributeName:2, NSUnderlineColorAttributeName:UIColor.redColor()]
let attrString01 = NSAttributedString(string: originString, attributes: attr01)
let attr02 = [NSBaselineOffsetAttributeName:0, NSUnderlineStyleAttributeName:2, NSUnderlineColorAttributeName:UIColor.yellowColor()]
let attrString02 = NSAttributedString(string: originString, attributes: attr02)
let attr03 = [NSBaselineOffsetAttributeName:-15, NSUnderlineStyleAttributeName:2, NSUnderlineColorAttributeName:UIColor.greenColor()]
let attrString03 = NSAttributedString(string: originString, attributes: attr03)
attrString.appendAttributedString(attrString01)
attrString.appendAttributedString(attrString02)
attrString.appendAttributedString(attrString03)
fontTextView.attributedText = attrString

font10.png
7.倾斜
NSObliquenessAttributeName

正值右倾,负值左倾

let originString = "String文本  "
let attrString = NSMutableAttributedString()
let attr01 = [NSObliquenessAttributeName:-1, NSUnderlineStyleAttributeName:2, NSUnderlineColorAttributeName:UIColor.redColor()]
let attrString01 = NSAttributedString(string: originString, attributes: attr01)
let attr02 = [NSObliquenessAttributeName:0, NSUnderlineStyleAttributeName:2, NSUnderlineColorAttributeName:UIColor.yellowColor()]
let attrString02 = NSAttributedString(string: originString, attributes: attr02)
let attr03 = [NSObliquenessAttributeName:1, NSUnderlineStyleAttributeName:2, NSUnderlineColorAttributeName:UIColor.greenColor()]
let attrString03 = NSAttributedString(string: originString, attributes: attr03)
attrString.appendAttributedString(attrString01)
attrString.appendAttributedString(attrString02)
attrString.appendAttributedString(attrString03)
fontTextView.attributedText = attrString

font11.png
8.拉伸
NSExpansionAttributeName
let originString = "String文本  "
let attrString = NSMutableAttributedString()
let attr01 = [NSExpansionAttributeName:-0.5, NSUnderlineStyleAttributeName:2, NSUnderlineColorAttributeName:UIColor.redColor()]
let attrString01 = NSAttributedString(string: originString, attributes: attr01)
let attr02 = [NSExpansionAttributeName:0, NSUnderlineStyleAttributeName:2, NSUnderlineColorAttributeName:UIColor.yellowColor()]
let attrString02 = NSAttributedString(string: originString, attributes: attr02)
let attr03 = [NSExpansionAttributeName:0.5, NSUnderlineStyleAttributeName:2, NSUnderlineColorAttributeName:UIColor.greenColor()]
let attrString03 = NSAttributedString(string: originString, attributes: attr03)
attrString.appendAttributedString(attrString01)
attrString.appendAttributedString(attrString02)
attrString.appendAttributedString(attrString03)
fontTextView.attributedText = attrString

font12.png
9.顺序
NSWritingDirectionAttributeName

值有四种组合

let a1 = [NSWritingDirection.LeftToRight.rawValue|NSWritingDirectionFormatType.Override.rawValue]
let a2 = [NSWritingDirection.LeftToRight.rawValue|NSWritingDirectionFormatType.Embedding.rawValue]
let a3 = [NSWritingDirection.RightToLeft.rawValue|NSWritingDirectionFormatType.Override.rawValue]
let a4 = [NSWritingDirection.RightToLeft.rawValue|NSWritingDirectionFormatType.Embedding.rawValue]

测试后,只有a3将文本顺序倒序了
其他三个可能在和其他属性一起用时有效果,还没有验证.

let originString = "String文本  "
let attr = [NSWritingDirectionAttributeName:a3]
let attrString = NSAttributedString(string: originString, attributes: attr)
fontTextView.attributedText = attrString

font13.png
10.描边
NSStrokeColorAttributeName
NSStrokeWidthAttributeName //正值中间为镂空效果,负值中间为填充效果
let originString = "String文本\n"
let attrString = NSMutableAttributedString()
let attr01 = [NSStrokeWidthAttributeName:-5, NSStrokeColorAttributeName:UIColor.purpleColor(), NSUnderlineColorAttributeName:UIColor.yellowColor(),NSFontAttributeName:UIFont.systemFontOfSize(50), NSForegroundColorAttributeName:UIColor.greenColor(),NSBackgroundColorAttributeName:UIColor.yellowColor()]
let attrString01 = NSAttributedString(string: originString, attributes: attr01)
let attr02 = [NSStrokeWidthAttributeName:0, NSStrokeColorAttributeName:UIColor.purpleColor(), NSUnderlineColorAttributeName:UIColor.yellowColor(),NSFontAttributeName:UIFont.systemFontOfSize(50),NSForegroundColorAttributeName:UIColor.greenColor()]
let attrString02 = NSAttributedString(string: originString, attributes: attr02)
let attr03 = [NSStrokeWidthAttributeName:5, NSStrokeColorAttributeName:UIColor.purpleColor(), NSUnderlineColorAttributeName:UIColor.greenColor(),NSFontAttributeName:UIFont.systemFontOfSize(50),NSForegroundColorAttributeName:UIColor.greenColor(), NSBackgroundColorAttributeName:UIColor.yellowColor()]
let attrString03 = NSAttributedString(string: originString, attributes: attr03)
attrString.appendAttributedString(attrString01)
attrString.appendAttributedString(attrString02)
attrString.appendAttributedString(attrString03)
fontTextView.attributedText = attrString

font14.png
11.图文混排
方法一:
let originString01 = "String"
let originString02 = "文本"
let attrString = NSMutableAttributedString()
let attrString01 = NSAttributedString(string: originString01, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(25)])
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "123.jpg")
attachment.bounds = CGRectMake(0, 0, 100, 60)
let attrString02 = NSAttributedString(attachment: attachment)
let attrString03 = NSAttributedString(string: originString02, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(25)])
attrString.appendAttributedString(attrString01)
attrString.appendAttributedString(attrString02)
attrString.appendAttributedString(attrString03)
fontTextView.attributedText = attrString
方法二
let originString = "String文本"
let attrString = NSMutableAttributedString(string: originString)
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "123.jpg")
attachment.bounds = CGRectMake(0, 0, 100, 60)
attrString.addAttributes([NSFontAttributeName:UIFont.systemFontOfSize(25)], range: NSMakeRange(0, originString.characters.count))
let pic = NSAttributedString(attachment: attachment)
attrString.insertAttributedString(pic, atIndex: 6)
fontTextView.attributedText = attrString

font15.png
12.连体
NSLigatureAttributeName

不是所有字符之间都有连体效果,少数几种字体有.
如 Heiti SC 的 fl 和fi之间
0无连体效果,1连体效果

 let originString = "fl  fi\n"
let attrString = NSMutableAttributedString()
let font = UIFont(name: "Heiti SC", size: 30)!
let attrStr01 = NSAttributedString(string: originString, attributes: [NSLigatureAttributeName:0, NSFontAttributeName:font])
let attrStr02 = NSAttributedString(string: originString, attributes: [NSLigatureAttributeName:1, NSFontAttributeName:font])
attrString.appendAttributedString(attrStr01)
attrString.appendAttributedString(attrStr02)
fontTextView.attributedText = attrString

font16.png
13.特效
NSTextEffectAttributeName
let originString = "String文本\n"
let attrString = NSMutableAttributedString()
let font = UIFont.systemFontOfSize(30)
let attrStr01 = NSAttributedString(string: originString, attributes: [ NSFontAttributeName:font])
let attrStr02 = NSAttributedString(string: originString, attributes: [NSTextEffectAttributeName:NSTextEffectLetterpressStyle, NSFontAttributeName:font])
attrString.appendAttributedString(attrStr01)
attrString.appendAttributedString(attrStr02)
fontTextView.attributedText = attrString

下面的是添加了特效的,对比起来还是有点区别


font17.png
14.段落
NSParagraphStyleAttributeName:NSParagraphStyle

可以类比word中的段落属性来理解
以下例子中,文字加颜色和下划线是为了更好的表示文字边界

1.行间距,首行缩进
lineSpacing //行间距
firstLineHeadIndent //首行缩进
let path = NSBundle.mainBundle().pathForResource("hupan", ofType: "txt")!
let originString = try? NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) as String
let paragraphStyle01 = NSMutableParagraphStyle()
paragraphStyle01.lineSpacing = -10
paragraphStyle01.firstLineHeadIndent = 0 //默认0
let attr01 = [NSParagraphStyleAttributeName:paragraphStyle01, NSBackgroundColorAttributeName:UIColor.cyanColor(), NSUnderlineStyleAttributeName:1, NSUnderlineColorAttributeName:UIColor.purpleColor(), NSFontAttributeName:UIFont.systemFontOfSize(20)]
let attrString01 = NSAttributedString(string: originString!, attributes: attr01)
fontTextView01.attributedText = attrString01;
//++++++++++++++++++++++++++++++
let paragraphStyle02 = NSMutableParagraphStyle()
paragraphStyle02.lineSpacing = -40 //负值无效果
paragraphStyle02.firstLineHeadIndent = 0
let attr02 = [NSParagraphStyleAttributeName:paragraphStyle02, NSBackgroundColorAttributeName:UIColor.cyanColor(), NSUnderlineStyleAttributeName:1, NSUnderlineColorAttributeName:UIColor.purpleColor(), NSFontAttributeName:UIFont.systemFontOfSize(20)]
let attrString02 = NSAttributedString(string: originString!, attributes: attr02)
fontTextView02.attributedText = attrString02;
//++++++++++++++++++++++++++++++
let paragraphStyle03 = NSMutableParagraphStyle()
paragraphStyle03.lineSpacing = 50 //行间距
paragraphStyle03.firstLineHeadIndent = 40
let attr03 = [NSParagraphStyleAttributeName:paragraphStyle03, NSBackgroundColorAttributeName:UIColor.cyanColor(), NSUnderlineStyleAttributeName:1, NSUnderlineColorAttributeName:UIColor.purpleColor(), NSFontAttributeName:UIFont.systemFontOfSize(20)]
let attrString03 = NSAttributedString(string: originString!, attributes: attr03)
fontTextView03.attributedText = attrString03;

font19_MarkMan.png
2.除首行外缩进,文本区域宽度
headIndent//直接翻译是"文本区域边缘到段落起始位置的距离",对首行无效
tailIndent//直接翻译是"文本区域边缘到段落结束位置的距离"
let path = NSBundle.mainBundle().pathForResource("hupan", ofType: "txt")!
let originString = try? NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) as String
let paragraphStyle01 = NSMutableParagraphStyle()
paragraphStyle01.headIndent = 20//除首行缩进
paragraphStyle01.tailIndent = 200 //文本区域宽度
let attr01 = [NSParagraphStyleAttributeName:paragraphStyle01, NSBackgroundColorAttributeName:UIColor.cyanColor(), NSUnderlineStyleAttributeName:1, NSUnderlineColorAttributeName:UIColor.purpleColor(), NSFontAttributeName:UIFont.systemFontOfSize(15)]
let attrString01 = NSAttributedString(string: originString!, attributes: attr01)
fontTextView01.attributedText = attrString01;
//++++++++++++++++++++++++++++++
let paragraphStyle02 = NSMutableParagraphStyle()
paragraphStyle02.headIndent = 50//除首行缩进
paragraphStyle02.tailIndent = 300
let attr02 = [NSParagraphStyleAttributeName:paragraphStyle02, NSBackgroundColorAttributeName:UIColor.cyanColor(), NSUnderlineStyleAttributeName:1, NSUnderlineColorAttributeName:UIColor.purpleColor(), NSFontAttributeName:UIFont.systemFontOfSize(15)]
let attrString02 = NSAttributedString(string: originString!, attributes: attr02)
fontTextView02.attributedText = attrString02;
//++++++++++++++++++++++++++++++
let paragraphStyle03 = NSMutableParagraphStyle()
paragraphStyle03.headIndent = 50//除首行缩进
paragraphStyle03.tailIndent = 0//默认全部宽度
let attr03 = [NSParagraphStyleAttributeName:paragraphStyle03, NSBackgroundColorAttributeName:UIColor.cyanColor(), NSUnderlineStyleAttributeName:1, NSUnderlineColorAttributeName:UIColor.purpleColor(), NSFontAttributeName:UIFont.systemFontOfSize(15)]
let attrString03 = NSAttributedString(string: originString!, attributes: attr03)
fontTextView03.attributedText = attrString03;

font20_MarkMan.png
3.段间距
paragraphSpacing
paragraphSpacingBefore
let path = NSBundle.mainBundle().pathForResource("hupan", ofType: "txt")!
let originString = try? NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) as String
let paragraphStyle01 = NSMutableParagraphStyle()
paragraphStyle01.paragraphSpacing = 40
paragraphStyle01.firstLineHeadIndent = 30
paragraphStyle01.paragraphSpacingBefore = 0
let attr01 = [NSParagraphStyleAttributeName:paragraphStyle01, NSBackgroundColorAttributeName:UIColor.cyanColor(), NSUnderlineStyleAttributeName:1, NSUnderlineColorAttributeName:UIColor.purpleColor(), NSFontAttributeName:UIFont.systemFontOfSize(15)]
let attrString01 = NSAttributedString(string: originString!, attributes: attr01)
fontTextView01.attributedText = attrString01;
//++++++++++++++++++++++++++++++
let paragraphStyle02 = NSMutableParagraphStyle()
paragraphStyle02.lineSpacing = 0
paragraphStyle02.paragraphSpacing = -30
paragraphStyle02.firstLineHeadIndent = 30
paragraphStyle02.paragraphSpacingBefore = 50
let attr02 = [NSParagraphStyleAttributeName:paragraphStyle02, NSBackgroundColorAttributeName:UIColor.cyanColor(), NSUnderlineStyleAttributeName:1, NSUnderlineColorAttributeName:UIColor.purpleColor(), NSFontAttributeName:UIFont.systemFontOfSize(15)]
let attrString02 = NSAttributedString(string: originString!, attributes: attr02)
fontTextView02.attributedText = attrString02;
//++++++++++++++++++++++++++++++
let paragraphStyle03 = NSMutableParagraphStyle()
paragraphStyle03.paragraphSpacing = 30
paragraphStyle03.firstLineHeadIndent = 40
paragraphStyle03.paragraphSpacingBefore = 50
let attr03 = [NSParagraphStyleAttributeName:paragraphStyle03, NSBackgroundColorAttributeName:UIColor.cyanColor(), NSUnderlineStyleAttributeName:1, NSUnderlineColorAttributeName:UIColor.purpleColor(), NSFontAttributeName:UIFont.systemFontOfSize(15)]
let attrString03 = NSAttributedString(string: originString!, attributes: attr03)
fontTextView03.attributedText = attrString03;

font21_MarkMan.png
4.排版方向
baseWritingDirection //排版方向
let path = NSBundle.mainBundle().pathForResource("hupan", ofType: "txt")!
let originString = try? NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) as String
let paragraphStyle01 = NSMutableParagraphStyle()
paragraphStyle01.firstLineHeadIndent = 30
paragraphStyle01.baseWritingDirection = .LeftToRight
let attr01 = [NSParagraphStyleAttributeName:paragraphStyle01, NSBackgroundColorAttributeName:UIColor.cyanColor(), NSUnderlineStyleAttributeName:1, NSUnderlineColorAttributeName:UIColor.purpleColor(), NSFontAttributeName:UIFont.systemFontOfSize(15)]
let attrString01 = NSAttributedString(string: originString!, attributes: attr01)
fontTextView01.attributedText = attrString01;
//++++++++++++++++++++++++++++++
let paragraphStyle02 = NSMutableParagraphStyle()
paragraphStyle02.firstLineHeadIndent = 30
paragraphStyle02.baseWritingDirection = .Natural
let attr02 = [NSParagraphStyleAttributeName:paragraphStyle02, NSBackgroundColorAttributeName:UIColor.cyanColor(), NSUnderlineStyleAttributeName:1, NSUnderlineColorAttributeName:UIColor.purpleColor(), NSFontAttributeName:UIFont.systemFontOfSize(15)]
let attrString02 = NSAttributedString(string: originString!, attributes: attr02)
fontTextView02.attributedText = attrString02;
//++++++++++++++++++++++++++++++
let paragraphStyle03 = NSMutableParagraphStyle()
paragraphStyle03.firstLineHeadIndent = 30
paragraphStyle03.baseWritingDirection = .RightToLeft
let attr03 = [NSParagraphStyleAttributeName:paragraphStyle03, NSBackgroundColorAttributeName:UIColor.cyanColor(), NSUnderlineStyleAttributeName:1, NSUnderlineColorAttributeName:UIColor.purpleColor(), NSFontAttributeName:UIFont.systemFontOfSize(15)]
let attrString03 = NSAttributedString(string: originString!, attributes: attr03)
fontTextView03.attributedText = attrString03;

font22_MarkMan.png
5.对齐方式
alignment

对应的是以下几种


font23.png
6.打断方式
lineBreakMode

与UILabel的属性类似

7.几倍行高,最大行高.最小行高
lineHeightMultiple //类比word
maximumLineHeight //最大行高.值小于默认行高,实际行高为最大行高;值大于默认行高,实际行高为默认行高
minimumLineHeight //最小行高.值大于默认行高,实际行高为最小行高;值小于默认行高,实际行高为默认行高

4.玩出花的富文本轮子

研究透一个肯定会提高很多
YYText:https://github.com/ibireme/YYText
TTTAttributedLabel:https://github.com/TTTAttributedLabel/TTTAttributedLabel
SlackTextViewController:https://github.com/slackhq/SlackTextViewController
JVFloatLabeledTextField:https://github.com/jverdi/JVFloatLabeledTextField
TOMSMorphingLabel:https://github.com/tomknig/TOMSMorphingLabel
ZSSRichTextEditor:https://github.com/nnhubbard/ZSSRichTextEditor
jot:https://github.com/IFTTT/jot

这篇关于iOS-NSAttributedString属性关键字的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1093857

相关文章

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

滚雪球学Java(87):Java事务处理:JDBC的ACID属性与实战技巧!真有两下子!

咦咦咦,各位小可爱,我是你们的好伙伴——bug菌,今天又来给大家普及Java SE啦,别躲起来啊,听我讲干货还不快点赞,赞多了我就有动力讲得更嗨啦!所以呀,养成先点赞后阅读的好习惯,别被干货淹没了哦~ 🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,助你一臂之力,带你早日登顶🚀,欢迎大家关注&&收藏!持续更新中,up!up!up!! 环境说明:Windows 10

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序

Oracle Start With关键字

Oracle Start With关键字 前言 旨在记录一些Oracle使用中遇到的各种各样的问题. 同时希望能帮到和我遇到同样问题的人. Start With (树查询) 问题描述: 在数据库中, 有一种比较常见得 设计模式, 层级结构 设计模式, 具体到 Oracle table中, 字段特点如下: ID, DSC, PID; 三个字段, 分别表示 当前标识的 ID(主键), DSC 当

关键字synchronized、volatile的比较

关键字volatile是线程同步的轻量级实现,所以volatile性能肯定比synchronized要好,并且volatile只能修饰于变量,而synchronized可以修饰方法,以及代码块。随着JDK新版本的发布,synchronized关键字的执行效率上得到很大提升,在开发中使用synchronized关键字的比率还是比较大的。多线程访问volatile不会发生阻塞,而synchronize

HTML5自定义属性对象Dataset

原文转自HTML5自定义属性对象Dataset简介 一、html5 自定义属性介绍 之前翻译的“你必须知道的28个HTML5特征、窍门和技术”一文中对于HTML5中自定义合法属性data-已经做过些介绍,就是在HTML5中我们可以使用data-前缀设置我们需要的自定义属性,来进行一些数据的存放,例如我们要在一个文字按钮上存放相对应的id: <a href="javascript:" d

Python中的属性装饰器:解锁更优雅的编程之道

引言 在Python的世界里,装饰器是一个强大的工具,它允许我们以一种非侵入性的方式修改函数或方法的行为。而当我们谈论“属性装饰器”时,则是在探讨如何使用装饰器来增强类中属性的功能。这不仅让我们的代码更加简洁、易读,同时也提供了强大的功能扩展能力。本文将带你深入了解属性装饰器的核心概念,并通过一系列实例展示其在不同场景下的应用,从基础到进阶,再到实际项目的实战经验分享,帮助你解锁Python编程

JavaScript 根据关键字匹配数组项

要在JavaScript数组中根据关键字匹配项,可以使用filter方法结合一个测试函数。以下是一个示例代码,定义了一个函数findByKeyword,该函数接受一个数组和一个关键字,然后返回一个新数组,其中包含与关键字匹配的所有项。 function findByKeyword(array, keyword) {return array.filter(item => {// 假设要匹配的是对象

react笔记 8-17 属性绑定 class绑定 引入图片 循环遍历

1、绑定属性 constructor(){super()this.state={name:"张三",title:'我是一个title'}}render() {return (<div><div>aaaaaaa{this.state.name}<div title={this.state.title}>我是一个title</div></div></div>)} 绑定属性直接使用花括号{}   注

ConstraintLayout布局里的一个属性app:layout_constraintDimensionRatio

ConstraintLayout 这是一个约束布局,可以尽可能的减少布局的嵌套。有一个属性特别好用,可以用来动态限制宽或者高app:layout_constraintDimensionRatio 关于app:layout_constraintDimensionRatio参数 app:layout_constraintDimensionRatio=“h,1:1” 表示高度height是动态变化