iPhone/iPad:如何确切使用NSATTRIBUTEDSTRING?
-
05-10-2019 - |
题
是的,许多人在iPhone/iPad中谈论丰富的文字,许多人知道 NSAttributedString
.
但是如何使用 NSAttributedString
?我搜索了很多时间,没有提取线索。
我知道如何设置 NSAttributedString
, ,那么我该怎么做才能在带有丰富文本的iPhone/iPad上显示文本?
官方文档说应该与 CoreText.Framework
, , 这意味着什么?
有什么简单的方法吗?
NSAttributedString *str;
.....
UILabel *label;
label.attributedString = str;
解决方案
你应该看看 Alisoftware的OhattributedLabel. 。它是Uilabel的一个子类,它绘制了NSATTRIBUTEDSTRING,还提供了方便方法来设置Uikit类的NSAttributedString的属性。
从存储库中提供的样本中:
#import "NSAttributedString+Attributes.h"
#import "OHAttributedLabel.h"
/**(1)** Build the NSAttributedString *******/
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:@"Hello World!"];
// for those calls we don't specify a range so it affects the whole string
[attrStr setFont:[UIFont systemFontOfSize:12]];
[attrStr setTextColor:[UIColor grayColor]];
// now we only change the color of "Hello"
[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(0,5)];
/**(2)** Affect the NSAttributedString to the OHAttributedLabel *******/
myAttributedLabel.attributedText = attrStr;
// Use the "Justified" alignment
myAttributedLabel.textAlignment = UITextAlignmentJustify;
// "Hello World!" will be displayed in the label, justified, "Hello" in red and " World!" in gray.
笔记: 在iOS 6+中,您可以使用 属性文本 Uilabel的财产。
其他提示
从iOS 6.0开始,您可以这样做:
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Hello. That is a test attributed string."];
[str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(3,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(10,7)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)];
label.attributedText = str;
你应该试试 tttattribedlabel. 。这是适用于uilabel的替换,可与NsattributedString一起使用,并且具有足够的性能。
有什么简单的方法
nsattribedstring *str;
Uilabel *标签;
label.AttributedString = str;
几乎。只需使用catextlayer即可。它有一个 string
您可以将其设置为nsattribedstring的属性。
编辑(2012年11月):当然,所有这些都在iOS 6中发生了变化。 attributedText
.
iOS 6上的uilabel归因于文本对齐的答案:使用nsmutableAbeatTributtedString,然后将nsmutableParagragraphAraphAraphAraphAllStyle添加到属性中。这样的事情:
NSString *str = @"Hello World!";
NSRange strRange = NSMakeRange(0, str.length);
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setAlignment:NSTextAlignmentCenter];
[attributedStr addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:strRange];
myUILabel.attributedText = attributedStr;
我认为给出解析(简化)HTML字符串的示例以创建NSATTRIBDERTSTRING将是有用的。
它还不完整 - 它仅处理u003Cb>和u003Ci>标签,对于初学者,并且不会打扰任何错误处理 - 但希望也是一个有用的例子,说明如何开始使用NSXMLPARSERDELEGATE ...u003C/i>u003C/b>
@interface ExampleHTMLStringToAttributedString : NSObject<NSXMLParserDelegate>
+(NSAttributedString*) getAttributedStringForHTMLText:(NSString*)htmlText WithFontSize:(CGFloat)fontSize;
@end
@interface ExampleHTMLStringToAttributedString()
@property NSString *mpString;
@property NSMutableAttributedString *mpAttributedString;
@property CGFloat mfFontSize;
@property NSMutableString *appendThisString;
@property BOOL mbIsBold;
@property BOOL mbIsItalic;
@end
@implementation ExampleHTMLStringToAttributedString
@synthesize mpString;
@synthesize mfFontSize;
@synthesize mpAttributedString;
@synthesize appendThisString;
@synthesize mbIsBold;
@synthesize mbIsItalic;
+(NSAttributedString*) getAttributedStringForHTMLText:(NSString*)htmlText WithFontSize:(CGFloat)fontSize {
ExampleHTMLStringToAttributedString *me = [[ExampleHTMLStringToAttributedString alloc] initWithString:htmlText];
return [me getAttributedStringWithFontSize:fontSize];
}
- (id)initWithString:(NSString*)inString {
self = [super init];
if (self) {
if ([inString hasPrefix:@""]) {
mpString = inString;
} else {
mpString = [NSString stringWithFormat:@"%@", inString];
}
mpAttributedString = [NSMutableAttributedString new];
}
return self;
}
-(NSAttributedString*) getAttributedStringWithFontSize:(CGFloat)fontSize {
mfFontSize = fontSize;
// Parse the XML
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[mpString dataUsingEncoding:NSUTF8StringEncoding]];
parser.delegate = self;
if (![parser parse]) {
return nil;
}
return mpAttributedString;
}
-(void) appendTheAccumulatedText {
UIFont *theFont = nil;
if (mbIsBold && mbIsItalic) {
// http://stackoverflow.com/questions/1384181/italic-bold-and-underlined-font-on-iphone
theFont = [UIFont fontWithName:@"Helvetica-BoldOblique" size:mfFontSize];
} else if (mbIsBold) {
theFont = [UIFont boldSystemFontOfSize:mfFontSize];
} else if (mbIsItalic) {
theFont = [UIFont italicSystemFontOfSize:mfFontSize];
} else {
theFont = [UIFont systemFontOfSize:mfFontSize];
}
NSAttributedString *appendThisAttributedString =
[[NSAttributedString alloc]
initWithString:appendThisString
attributes:@{NSFontAttributeName : theFont}];
[mpAttributedString appendAttributedString:appendThisAttributedString];
[appendThisString setString:@""];
}
#pragma NSXMLParserDelegate delegate
-(void)parserDidStartDocument:(NSXMLParser *)parser{
appendThisString = [NSMutableString new];
mbIsBold = NO;
mbIsItalic = NO;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"body"]){
} else if ([elementName isEqualToString:@"i"]) {
[self appendTheAccumulatedText];
mbIsItalic = YES;
} else if ([elementName isEqualToString:@"b"]) {
[self appendTheAccumulatedText];
mbIsBold = YES;
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"body"]){
[self appendTheAccumulatedText];
} else if ([elementName isEqualToString:@"i"]) {
[self appendTheAccumulatedText];
mbIsItalic = NO;
} else if ([elementName isEqualToString:@"b"]) {
[self appendTheAccumulatedText];
mbIsBold = NO;
}
}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
[appendThisString appendString:string];
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
}
@end
要使用这样的事情:
self.myTextView.attributedText = [ExampleHTMLStringToAttributedString getAttributedStringForHTMLText:@"this is <b>bold</b> text" WithFontSize:self.myTextView.pointSize];
从iOS 6.0开始,您可以这样做:另一个示例代码。
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"This is my test code to test this label style is working or not on the text to show other user"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,31)];
[str addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(61,10)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica-Bold" size:13.0] range:NSMakeRange(32, 28)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:13.0] range:NSMakeRange(65, 20)];
_textLabel.attributedText = str;
为了 迅速 用这个,
它会做到 titl 文本大胆,
var title = NSMutableAttributedString(string: "Title Text")
title.addAttributes([NSFontAttributeName: UIFont(name: "AvenirNext-Bold", size: iCurrentFontSize)!], range: NSMakeRange(0, 4))
label.attributedText = title
我知道这有点晚,但是对他人很有用,
NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:@"string" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
[self.label setAttributedText:newString];
将所需属性添加到字典中,并将其作为属性参数传递