通过查看其他一些SO问题,似乎其他人在获取kCTSuperscriptAttributeName以正确显示上标和下标时遇到问题.我一直无法找到kCTSuperscriptAttributeName的任何成功实现,即使Apple的文档声称它有效.
我试过这个,但xCode显示错误,将其列为"未声明的标识符"
NSDictionary * superscriptAttrs = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:kCTSuperscriptAttributeName];
Run Code Online (Sandbox Code Playgroud)
有些人还声称可以通过WebViews制作上标和下标,但我的下标/上标需要是动态的,所以我担心这种方法会使事情复杂化.
要么实现kSupersciptAttributeName的正确方法,要么我可以采取替代路线来制作脚本.谢谢
我在IB中有一个绑定到控制器的标签(NSTextField).控制器在awakeFromNIB上,将标签的attributedStringValue设置为包含一些彩色文本和一个或两个链接.
当您看到标签时,它包含正确的字符串值,但某些格式会丢失 - 直到您单击标签,并且它会更新为包含正确的格式.
我正在使用此代码来设置值:
[self.testTextField setAllowsEditingTextAttributes:YES];
[self.testTextField setSelectable:YES];
NSMutableAttributedString *linkString = [[NSMutableAttributedString alloc] initWithString:@"hit this "];
[linkString beginEditing];
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:@"link"];
NSRange range = NSMakeRange(0, [attrString length]);
[attrString addAttribute:NSLinkAttributeName value:[[NSURL URLWithString:@"http://google.com"] absoluteString] range:range];
[attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlinePatternDot] range:range];
[attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blackColor] range:range];
[linkString appendAttributedString:attrString];
[linkString appendAttributedString:[[NSAttributedString alloc] initWithString:@" to search"]];
[linkString addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(0, [linkString length])];
[linkString endEditing];
[self.testTextField setAttributedStringValue:linkString];
Run Code Online (Sandbox Code Playgroud)
基于此示例,您将看到红色字符串和默认Label字体.然后,当您单击标签时,字体会更改大小和面部,并且链接会神奇地呈现.
关于如何在第一次正确渲染字符串的任何想法?
比较或测试NSAttributed字符串的特定颜色属性的正确方法是什么?
作为一个例子,我想知道文本选择是否有红色文本.我已经尝试了几种方法,如下所示,但它们都没有产生匹配.我看到文本在屏幕上变为红色,并且记录该属性返回:UIDeviceRGBColorSpace 1 0 0 1
- (BOOL)isRedWithAttributes:(NSDictionary *)attributes
{
BOOL isRedWithAttributes = NO;
if (attributes != nil)
{
// if ( [attributes objectForKey:NSForegroundColorAttributeName] == [UIColor redColor] )
// if ( [attributes objectForKey:NSForegroundColorAttributeName] == @"UIDeviceRGBColorSpace 1 0 0 1" )
if ( [attributes objectForKey:NSForegroundColorAttributeName] == [UIColor colorWithRed:1 green:0 blue:0 alpha:1] )
{
isRedWithAttributes = YES;
}
else
{
isRedWithAttributes = NO;
}
}
return isRedWithAttributes;
}
Run Code Online (Sandbox Code Playgroud)
以下是我为测试传递属性的方法:
NSAttributedString *selectedString = [attributedString attributedSubstringFromRange:selectedRange];
[selectedString enumerateAttributesInRange:NSMakeRange(0, [selectedString length])
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
usingBlock:^(NSDictionary *attributes, NSRange …Run Code Online (Sandbox Code Playgroud) 我想在iOS上垂直绘制NSAttributedString(或NSString).通过"垂直"我的意思是:

Apple的文档说明了一个标准属性,但不幸的NSAttributedString是NSVerticalGlyphFormAttributeName,它说:
"在iOS中,始终使用水平文本,并且未定义指定不同的值."
同样的文件也提到NSTextLayoutSectionsAttribute,似乎支持NSTextLayoutOrientation,允许NSTextLayoutOrientationHorizontal或NSTextLayoutOrientationVertical.
这4个术语中没有一个在目前的SO上有任何命中.只是那种孤独的吹口哨声.
但是,我不知道如何设置它,是否适用于iOS,或者是否可以用于风格的垂直字符串渲染[myAttributedString drawAtPoint: whereItOughtaGo].
谢谢.
我已经看了大约10个关于SO的问题,而且我仍然很短.
我有一个多行UILabel(在Interface Builder中创建,numberOfLines设置为0),它通常像这样呈现:

我想强调"服务条款"和"隐私政策",所以我添加了以下代码:
NSString *text = self.agreement.text;
NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:text];
NSRange privacyRange = [text rangeOfString:@"privacy policy" options:NSCaseInsensitiveSearch];
[aString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:privacyRange];
NSRange tosRange = [text rangeOfString:@"terms of service" options:NSCaseInsensitiveSearch];
[aString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:tosRange];
self.agreement.attributedText = aString;
Run Code Online (Sandbox Code Playgroud)
但结果如下:

我需要做什么才能显示这两行,并用适当的范围加下划线?
此外,我不想使用像OHAttributedLabel或TTTAttributedLabel这样的第三方库,因为这是我的应用中唯一需要为一段文字加下划线的地方.
我试过的
sizeToFit设置属性文本后调用numberOfLines和lineBreakMode代码Sascha让我上传了两个截图,背景颜色设置为清除之外的其他颜色.奇怪的是,一切都按预期显示!不知道该怎么做或这告诉我们什么.

objective-c nsattributedstring uilabel ios nsmutableattributedstring
我这里有一个复杂的场景.
我有一个Web服务器将JSON数据发送回我的应用程序,其中一个数据是HTML文本,例如json:
...
{
id: 3,
name: "Content Title",
description: "<p>This is a paragraph block</p><p>This is another paragraph</p>",
}
Run Code Online (Sandbox Code Playgroud)
您可能知道,HTML标记文本会增加另一层复杂性,因为HTML标记会修改实际文本的高度.
所以像这样的字符串:
"Hello World"
Run Code Online (Sandbox Code Playgroud)
可能只有10像素高的普通字符串,但如果格式如下:
"<h1>Hello World</h1>"
Run Code Online (Sandbox Code Playgroud)
的真实高度可以潜在地是大于10像素高.
我想知道的是如何计算出真正的高度?
这就是我用归因字符串计算UILabel高度的方法,但它似乎只给出了适合文本像素的高度,而不是实际的HTML元素块大小:
-(CGFloat)dynamicHeightForHTMLAttributedString:(NSString *)dataString UsingWidth:(CGFloat)width AndFont:(UIFont *)font
{
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
NSMutableAttributedString *htmlString =
[[NSMutableAttributedString alloc] initWithData:[dataString dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]}
documentAttributes:NULL error:nil];
[htmlString addAttributes:attrsDictionary range:NSMakeRange(0, htmlString.length)];
CGRect rect = [htmlString boundingRectWithSize:(CGSize){width, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGSize size = rect.size;
return ceilf(size.height);
}
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?
我在使用"NSForegroundColorAttributeName"更改子字符串颜色时遇到问题.我很困惑,因为正在应用相同子字符串的其他属性(下划线).
还尝试使用弃用属性"UITextAttributeTextColor"和其他建议属性"kCTForegroundColorAttributeName"并获得相同的效果.
我正在为iOS 7编译.

NSString *freeText = [NSString stringWithFormat:@"(%@)", self.me.activity.text];
int lblMaxWidth = arrImgView.origin.x - WPRConstraints.BorderOffset;
int lblMaxHeight = self.activityView.size.height - WPRConstraints.BorderOffset * 2;
RevUILabel *addActivityLbl = [[RevUILabel alloc] initWithFontNameMultiLine:[WPRFonts LattoBold]
size:16
sizeConstrain:CGSizeMake(lblMaxWidth,lblMaxHeight)];
addActivityLbl.text = [NSString stringWithFormat:@"%@ %@", Translate(self.me.activity.activityKey), freeText] ;
addActivityLbl.textColor = BlackColor;
NSMutableAttributedString *str = [addActivityLbl.attributedText mutableCopy];
[str addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:[addActivityLbl.text rangeOfString:freeText]];
[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:1] range:[addActivityLbl.text rangeOfString:freeText]];
[str addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:[addActivityLbl.text rangeOfString:freeText]];
addActivityLbl.attributedText = str;
addActivityLbl.frame = CGRectMake(WPRConstraints.BorderOffset,
WPRConstraints.BorderOffset,
addActivityLbl.size.width,
addActivityLbl.size.height);
[self.activityView addSubview:addActivityLbl];
Run Code Online (Sandbox Code Playgroud) 根据NSHipster的说法,“使自定义UI类符合UIAppearance不仅是最佳实践,而且还表明在实现过程中要加倍注意。”
因此,我正在尝试将文本属性(稍后用于创建)设置为子类中NSAttributedString的属性var titleTextAttributes: [String : AnyObject]?,UIView如下所示:
func applyAppearance() {
UINavigationBar.appearance().translucent = true
UINavigationBar.appearance().tintColor = UIColor(named: .NavBarTextColor)
UINavigationBar.appearance().barTintColor = UIColor(named: .NavBarBlue)
UINavigationBar.appearance().titleTextAttributes = [
NSForegroundColorAttributeName: UIColor(named: .NavBarTextColor),
NSFontAttributeName: UIFont.navBarTitleFont()!
]
ActionBarView.appearance().backgroundColor = UIColor(white: 1, alpha: 0.15)
ActionBarView.appearance().titleTextAttributes = [
NSKernAttributeName: 1.29,
NSFontAttributeName: UIFont.buttonFont()!,
NSForegroundColorAttributeName: UIColor.whiteColor(),
]
}
Run Code Online (Sandbox Code Playgroud)
这是我的偷窥AppDelegate。
现在,当尝试设置ActionBarView.appearance().titleTextAttributes = [ ... ]时,出现以下运行时错误:
值得一提的是,在属性上设置属性UINavigationBar没有任何问题。
转到UINavigationBar的头文件将显示以下内容:
/* You may specify the font, text color, and shadow …Run Code Online (Sandbox Code Playgroud) 使用语法有点困惑.我知道你应该使用NSFontAttributeName,但我不知道如何正确指定范围.
我很好奇两个案例.
如何指定文本中所有字符的范围,以及如何将范围指定为前10个字符.
有什么建议?
let dictTitleColor = [NSAttributedStringKey.foregroundColor: UIColor.LTColor()]
let titleAttributedString = NSMutableAttributedString(string: title, attributes: dictTitleColor)
alert.setValue(titleAttributedString, forKey: "attributedTitle")
Run Code Online (Sandbox Code Playgroud)
当我增加设备的字体大小时标题字符串不增加我在alertview控制器中设置了这个字符串?那么如何使这个响应字体大小更改?
accessibility nsattributedstring ios swift dynamic-type-feature
ios ×8
objective-c ×4
swift ×3
autolayout ×1
cocoa ×1
compare ×1
core-text ×1
html ×1
ios6 ×1
ios7 ×1
nsdictionary ×1
nsstring ×1
nstextfield ×1
uiappearance ×1
uicolor ×1
uikit ×1
uilabel ×1