标签: nsattributedstring

CoreText.如何计算属性字符串的边界框?

在CoreText中,很容易问:"对于给定的矩形,这个属性字符串有多少适合?".

CTFrameGetVisibleStringRange(rect).length
Run Code Online (Sandbox Code Playgroud)

将返回字符串中的下一行文本应该开始的位置.

我的问题是:"给定一个属性字符串和宽度,我需要什么矩形高度来完全绑定属性字符串?".

CoreText框架是否提供了执行此操作的工具?

谢谢,
道格

nsattributedstring core-text

14
推荐指数
1
解决办法
5465
查看次数

UITextField的attributesPlaceholder无效

我试图在我的文本字段中使用占位符斜体,并且由于我的应用程序是针对iOS 6.0或更新版本,因此决定使用attributedPlaceholder属性而不是滚动更自定义的内容.代码如下:

NSString *plString = @"optional";
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:plString
        attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-LightItalic" size:15]}];
for (UITextField *t in myTextfields){
    t.placeholder = plString;
    t.attributedPlaceholder = placeholder;
}
Run Code Online (Sandbox Code Playgroud)

然而占位符的样式仍然不是斜体,而是与常规文本相同,只是更暗淡.我缺少什么才能完成这项NSAttributedString工作?

uitextfield nsattributedstring ios

14
推荐指数
3
解决办法
9257
查看次数

如何使NSStringDrawingContext缩小文本?

我正在尝试使用iOS 6的属性字符串API来计算文本的大小,并在必要时缩小字体大小.但是,正如文档所说,我无法让它工作.

NSString *string = @"This is a long text that doesn't shrink as it should";

NSStringDrawingContext *context = [NSStringDrawingContext new];
context.minimumScaleFactor = 0.5;

UIFont *font = [UIFont fontWithName:@"SourceSansPro-Bold" size:32.f];

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineBreakMode = NSLineBreakByClipping;

NSDictionary *attributes = @{ NSFontAttributeName: font,
                              NSParagraphStyleAttributeName: paragraphStyle };

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.title attributes:attributes];

CGRect rect = [attributedString boundingRectWithSize:CGSizeMake(512.f, 512.f) options:NSStringDrawingUsesLineFragmentOrigin context:context];

NSLog(@"rect: %@, context: %@", NSStringFromCGRect(rect), context.debugDescription);
Run Code Online (Sandbox Code Playgroud)

但是文本没有缩小并被截断.actualScaleFactor永远1.日志结果如下:

rect:{{0, 0}, {431.64801, 80.447998}}, context:<NSStringDrawingContext: 0x14e85770> minimumScaleFactor:0.500000 …
Run Code Online (Sandbox Code Playgroud)

text nsattributedstring ios

14
推荐指数
2
解决办法
5617
查看次数

从html创建nsattributedstring时,ios7字体大小会发生变化

我有一个UITextView,我正在管理一个NSAttributedString,最初通过键盘正常输入.我将属性字符串保存为HTML,看起来很好.当我再次加载它,并将其转换回HTML中的属性字符串时,字体大小似乎发生了变化.

例如,加载时的HTML如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 21.0px Helvetica; color: #000000; -webkit-text-        stroke: #000000}
span.s1 {font-family: 'Helvetica'; font-weight: normal; font-style: normal; font-size:     21.00pt;     font-kerning: none}
</style>
</head>
<body>
<p class="p1"><span class="s1">There is some text as usual lots of text</span></p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我转换它并使用以下代码检查属性:

    // convert to attributed string
    NSError *err;
    NSAttributedString *as = [[NSAttributedString alloc] …
Run Code Online (Sandbox Code Playgroud)

html fonts nsattributedstring ios7

14
推荐指数
2
解决办法
8351
查看次数

UILabel外观字体和属性字符串字体

在我的应用程序中,我将全局自定义字体应用于所有标签,如下所示:

UIFont *font = [UIFont fontWithName:kMyFontName size:15.0]; 
[[UILabel appearance] setFont:font];
Run Code Online (Sandbox Code Playgroud)

这很好用.但是,在某些情况下,我希望能够为UILabel字符串的特定区域指定不同的字体.

所以我有这样的事情:

NSString *string = @"Foo Bar Baz";
UIFont *boldFont = [UIFont fontWithName:kMyBoldFontName size:15.0]; 
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
[attrString setAttributes:@{ NSFontAttributeName: boldFont } range:NSMakeRange(0, 3)];
self.myLabel.attributedText = attrString;
Run Code Online (Sandbox Code Playgroud)

然而,这似乎不起作用.我希望"Foo"是粗体,但整个字符串只有默认字体.就好像粗体字体根本没有应用,并且被UILabel外观代理上的字体集覆盖.

当我删除UILabel外观线然后它工作正常(我可以看到粗体字符串的一部分).基本上我想将自定义字体应用于标签,但是应用于字符串的不同区域的单独字体.通常这适用于属性字符串但由于某种原因设置UILabel外观字体禁用此功能(或似乎如此).

  • 预期成果:" Foo Bar Baz"
  • 实际结果:"Foo Bar Baz"

如果我删除该[[UILabel appearance] setFont:]行,那么它的工作原理:

  • " Foo Bar Baz"

(但是字符串的其余部分没有设置自定义字体).

所以我的问题是:有没有办法指定一个字体用作默认的应用程序范围,但仍然可以使用属性字符串部分覆盖它?

此外,如果有人可以向我解释为什么这不起作用我会很感激.

objective-c nsattributedstring uilabel ios uiappearance

14
推荐指数
2
解决办法
1万
查看次数

防止部分NSAttributedString中的换行符

我正在开发一个UILabel具有大型主文本的文本,后跟较小的文本,告诉您是谁说的:

截图显示问题

现在它基本上是NSAttributedString小文本上的字体属性.

我想把事情做好,所以大文字包好,但小文字没有.也就是说,如果文本将与正确的项目中的所有内容相同,则它应该按原样呈现,但是它会像在左侧项目中一样包裹,整个小文本应该出现在下一行:

截图显示正确的行为

与我想要实现的HTML相当的是:

Title <nobr>Subtitle</nobr>
- or -
Title <span style="white-space:nowrap">Subtitle</span>
Run Code Online (Sandbox Code Playgroud)

我已经尝试将这两个转换为NSAttributedStrings,NSHTMLTextDocumentType它似乎没有直接翻译.

nsattributedstring ios

14
推荐指数
2
解决办法
3491
查看次数

在iOS 8上显示NSMutableAttributedString

iOS 8.0(12A365)上的似乎NSMutableAttributedString有时无法正确显示.当属性的范围不在文本的开头(并且如果在文本的开头没有其他属性开始)时,显然会出现问题.

所以1.)第二个单词"green"将不显示绿色背景(bug!)("cell"是UITableViewCell带有UILabel"label"作为子视图):

1.)

text = [[NSMutableAttributedString alloc] initWithString:@"Green is green. (-> Bug)"];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:(NSRange){9,5}];
cell.label.attributedText=text
Run Code Online (Sandbox Code Playgroud)

使用2.)和3.)正确显示背景:

2.)

text = [[NSMutableAttributedString alloc] initWithString:@"Green is green. (-> Ok)"];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:(NSRange){0,5}];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:(NSRange){9,5}];
cell.label.attributedText=text
Run Code Online (Sandbox Code Playgroud)

3.)

text = [[NSMutableAttributedString alloc] initWithString:@"Green is green. (-> Ok)"];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:(NSRange){0,5}];
cell.label.attributedText=text
Run Code Online (Sandbox Code Playgroud)

在此处查找屏幕截图和XCode 6项目:屏幕截图和XCode 6项目

对我来说似乎是iOS 8中的一个错误 - 所以报告发给Apple.

uitableview nsattributedstring uilabel xcode6 ios8

14
推荐指数
2
解决办法
5205
查看次数

如何使用NSMutableAttributedString呈现多行UILabel

我正在尝试使用NSMutableAttributedString创建多行UILabel.当我将属性分配给完整的字符串时,这样可以正常工作:

UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)];
[contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
[contentLabel setNumberOfLines:0];
[contentLabel setFont:[UIFont systemFontOfSize:13];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello I am a long sentence that should break over multiple lines"];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(0, [string length])];

contentLabel.attributedText = string;
Run Code Online (Sandbox Code Playgroud)

但我需要的是为NSAttributedString的不同子范围应用许多属性(以加粗某些单词).

UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)];
[contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
[contentLabel setNumberOfLines:0];
[contentLabel setFont:[UIFont systemFontOfSize:13];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello I am a long sentence that should break over multiple lines"];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(3, 5)];

contentLabel.attributedText = …
Run Code Online (Sandbox Code Playgroud)

objective-c multiline nsattributedstring uilabel ios6

13
推荐指数
1
解决办法
8638
查看次数

如何在DTCoreText中正确呈现嵌套/多个上标?

我希望使用DTCoreText从HTML到NSAttributedString有多个/嵌套的超.

如果我采用以下HTML:

Some text with a sup<sup>er<sup>scr<sup>ipt</sup></sup></sup>
Run Code Online (Sandbox Code Playgroud)

它在Stackoverflow上正确呈现为:

有些文字用燮SCR IPT

但在DTCoreText中,它呈现为:

在此输入图像描述

如果你发现它几乎以相反的顺序显示,那么上标就会下降.

如何正确显示?

objective-c uitextview nsattributedstring ios dtcoretext

13
推荐指数
1
解决办法
400
查看次数

NSAttributedString和emojis:位置和长度问题

我正在使用NSAttributedString为来自API的文本的某些部分(在Twitter上认为"@mention")着色.

API为我提供了文本和一组实体,这些实体表示应该着色的文本部分(或链接,标签等).

但有时,由于表情符号,颜色会被抵消.


例如,使用此文本:

"@ericd一些文字.@apero"

API给出:

[{"text":"ericd","len":6,"pos":0},{"text":"apero","len":6,"pos":18}]

我使用NSRange成功转换为NSAttributedString:

for m in entities.mentions {
    let r = NSMakeRange(m.pos, m.len)
    myAttributedString.addAttribute(NSForegroundColorAttributeName, value: someValue, range: r)
}
Run Code Online (Sandbox Code Playgroud)

我们认为这"pos": 18是正确的,这是"@apero"开始的地方.正如预期的那样,彩色部分是"@ericd"和"@apero".

但是当在文本中使用表情符号的某些特定组合时,API不能很好地转换为NSATtributedString,颜色是偏移的:

"@ericd一些文字.✌@apero"

得到:

[{"text":"ericd","len":6,"pos":0},{"text":"apero","len":6,"pos":22}]

"pos": 22:API作者声明这是正确的,我理解他们的观点.

不幸的是,NSAttributedString不同意,我的着色是关闭的:

在此输入图像描述

第二次提到的最后一个字符没有着色(因为因为表情符号,"pos"太短了?).

正如您可能已经猜到的那样,我无论如何都无法改变API的行为方式,我必须在客户端进行调整.

除此之外......我不知道该怎么做.我是否应该尝试检测文本中的表情符号,并在有问题的表情符号时手动修改提及的位置?但是,检测哪个表情符号改变位置以及哪个表情符号没有改变的标准是什么?以及如何确定我需要多少偏移?也许问题是由NSAttributedString引起的?

我知道这与表情符号的长度相比,与他们作为离散字符的长度相比,但是......嗯......我迷失了(叹气).


请注意,我已经尝试实现类似于这个东西的解决方案,因为我的API与此兼容,但它只是部分工作,一些表情符号仍在破坏索引:

在此输入图像描述

unicode nsattributedstring emoji nsrange swift

13
推荐指数
1
解决办法
2054
查看次数