我听说我可以使用CoreText显示NSAttributedString,谁能说我怎么样(最简单的方法)?
请不要使用CATextLayer或OHAttributedLabel回答.
我知道在这个论坛上有很多关于这方面的问题,但我找不到答案
谢谢!!
我正在创建具有标签的iOS应用程序.我想设置两种颜色.一个用于第一部分,另一个用于剩余部分.
我在Stack over流程中看到了一些消息,TTTAttributedLabel能够为文本设置多种颜色.我的文字就像"ABC> def".对于"ABC",我想设置棕色和"def",我想设置白色.
我怎么设置?
我试图在自定义UIButton上为我的标题添加下划线但是按照这里找到的示例,我无法在屏幕上显示它.
这是我尝试使用的代码:
NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"The Quick Brown Fox"];
[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];
[button setAttributedTitle:commentString forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
这没有显示出来.但是,如果我只是做一个常规的,像这样:
[button setTitle:@"The Quick Brown Fox" forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
这显示很好.有人能告诉我我错过了什么吗?
下载我创建的示例
我正在添加一个链接到我的UITextView,如下所示:
let systemFont = self.text.font!
let linkAttributes = [
NSFontAttributeName : systemFont,
NSLinkAttributeName: NSURL(string: alertController.textFields![0].text!)!] as [String : Any]
let myAttributes2 = [ NSForegroundColorAttributeName: customGreen]
let attributedString = NSMutableAttributedString(string: "\(urlName)")
// Set the 'click here' substring to be the link
attributedString.setAttributes(linkAttributes, range: NSMakeRange(0, urlName.characters.count))//(0, urlName.characters.count))
self.text.linkTextAttributes = myAttributes2
self.text.textStorage.insert(attributedString, at: self.text.selectedRange.location)
let cursor = NSRange(location: self.text.selectedRange.location + "\(urlName)".characters.count, length: 0)
self.text.selectedRange = cursor
// self.text.font = systemFont
Run Code Online (Sandbox Code Playgroud)
但插入后,它会将UITextView中的所有当前文本更改为相同的字体.
因此,例如,如果我有一些粗体文本和一些更多的文本是Italic,在我添加url(它是系统字体)后,它会将所有粗体/斜体文本重置为系统字体....
如果有人知道如何保留以前文本的当前字体,我真的很感激帮助.
如需进一步说明,请在下方发表评论!
非常感谢任何有帮助的人!
更新
我正在更改textDidChange中的文本
if text == " …Run Code Online (Sandbox Code Playgroud) 是否可以在同一个UILabel中使用不同的字体大小或重量?我可以在故事板中作为属性标签来做,但我需要以编程方式进行.
cell.lblOne.text = [NSString stringWithFormat:
@"FontSize15:: %@, FontSize20:: %@",monkey, goat];
Run Code Online (Sandbox Code Playgroud)
编辑:我看到了一些关于NSAttributedString但我不能让它工作的东西.
我有NSString,我需要制作NSAttributedString.
NSString是这样的:
bvcx b vcxbcvx bcxvbcxv bvx xbc bcvx bxcv bcxv bcxv bcxv bcvx bcvx bcxvbcvx bvc bcvx bxcv{
NSFont = "\"LucidaGrande 24.00 pt. P [] (0x108768a80) fobj=0x108788880, spc=7.59\"";
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0.05, HeaderLevel 0";
} …Run Code Online (Sandbox Code Playgroud) 我通过属性字符串属性在故事板中设置UITextView文本的外观.但是,当我尝试在运行时访问它以复制属性对象时,它返回null.
NSAttributedString *atrString = self.contentTextView.attributedText;
NSLog(@"atrString = %@", atrString);
Run Code Online (Sandbox Code Playgroud)
输出
2013-09-20 14:44:19.572 PageTest[69125:70b] atrString = (null)
Run Code Online (Sandbox Code Playgroud)
我之前没有使用过属性字符串所以我确定我做错了什么,但是搜索文档仍然让我空洞.任何援助将不胜感激.谢谢!
我正在尝试获取属性字符串的高度.这正如预期的那样:
[attrString boundingRectWithSize:size
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine)
context:NULL]
Run Code Online (Sandbox Code Playgroud)
...除非字符串包含表情符号.使用表情符号时,字符串会在标签底部被切掉.我需要做些什么吗?
iOS 7用于NSString::sizeWithAttributes计算NSString给定NSDictionary属性的边界框的大小.但是,我找不到列出可用于其中的所有枚举键的资源NSDictionary.
这些属性可以应用于NSAttributedString对象
但是没有列出它们,它们也没有在NSAttributedString文档中列出.
另外:可用的属性键,这是调整多线标签的最基本的?似乎字体大小和换行模式是最重要的,但没有可用选项的列表,很难说...
我有一个可编辑的UITextView,它加载了一个属性String.此属性String加载了应用程序设置的自定义属性.出于这个原因,我总是将这些自定义属性添加到typingAttributestextView.
每当我打字它按预期工作文本,所加入的文字具有自定义属性集,但每当我使用的预测建议更换一个字,整个单词的设置没有自定义属性.
据我所知,每次以编程方式设置属性文本时,都会typingAttributes自动清除.因此,当选择预测建议时,文本会以编程方式设置,因此我丢失了所有自定义属性.
有没有办法解决?
ios ×8
nsstring ×3
objective-c ×3
uilabel ×3
uitextview ×3
swift ×2
cocoa ×1
cocoa-touch ×1
core-text ×1
emoji ×1
ios6 ×1
iphone ×1
uibutton ×1
uifont ×1