iOS NSMutableAttributedString/NSAttributedString 富文本設置


  1. //UILabel 設置NSAttributedString  ios


  1. NSMutableParagraphStyle *ps = [[NSMutableParagraphStyle alloc] init];  app

  2.     [ps setAlignment:NSTextAlignmentCenter];  ide

  3.     NSDictionary *attribs = @{NSFontAttributeName: [UIFont fontWithName:@"Microsoft Yahei" size:45],NSParagraphStyleAttributeName:ps};  工具

  4.     NSMutableAttributedString *attributedText =[[NSMutableAttributedString alloc] initWithString:super.text  字體

  5.                                            attributes:attribs];  spa

  6.     super.attributedText = attributedText;  orm


UITextView *lab = [LocalTexts objectAtIndex:j];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineHeightMultiple = 50.0f;
paragraphStyle.maximumLineHeight = 50.0f;
paragraphStyle.minimumLineHeight = 50.0f;

NSString *string = lab.text;
NSDictionary *ats = @{
NSFontAttributeName : [UIFont fontWithName:@"DIN Medium" size:16.0f],
NSParagraphStyleAttributeName : paragraphStyle, 
};
lab.attributedText = [[NSAttributedString alloc] initWithString:string attributes:ats];//textview 設置行間距


NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentJustified;//設置對齊方式
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
ip


NSAttributedString的初始化方法有ci



-initWithString:用String初始化,並無Attributed信息。


-initWithAttributedString:用AttributedString去初始化。


-initWithString:Attributed:用string及attribute的dictionary來初始化。


具體AttributtedString屬性的鍵值對以下:


NSString *const NSFontAttributeName;//值爲UIFont,設置字體,默認值爲12-point Helvetica(Neue) 。


下面這段代碼能夠查看ios中可用的字體,具體那些字體長什麼樣,能夠查看字體冊工具。


NSArray *familyArray = [UIFont familyNames];


for (id family in familyArray) {


printf(「%s\n」,[family cStringUsingEncoding:NSUTF8StringEncoding]);


NSArray *fontArray = [UIFont fontNamesForFamilyName:family];


for (id font in fontArray) {


printf(」   %s\n」,[font cStringUsingEncoding:NSUTF8StringEncoding]);


}
}


NSString *const NSParagraphStyleAttributeName;//值爲NSParagraphStyle,設置段落屬性,默認值爲[NSParagraphStyle defaultParagraphStyle]返回的值。


NSMutableParagraphStyle與NSParagraphStyle包括一下屬性


  alignment //對齊方式
  firstLineHeadIndent //首行縮進
  headIndent //縮進
  tailIndent  //尾部縮進
  lineBreakMode  //斷行方式
  maximumLineHeight  //最大行高
  minimumLineHeight  //最低行高
  lineSpacing  //行距
  paragraphSpacing  //段距
  paragraphSpacingBefore  //段首空間
  baseWritingDirection  //句子方向
  lineHeightMultiple  //可變行高,乘因數。
  hyphenationFactor //連字符屬性
NSString *const NSForegroundColorAttributeName;//值爲UIColor,字體顏色,默認爲黑色。


NSString *const NSBackgroundColorAttributeName;//值爲UIColor,字體背景色,默認沒有。


NSString *const NSLigatureAttributeName;//值爲整型NSNumber,連字屬性,通常中文用不到,在英文中可能出現相鄰字母連筆的狀況。0爲不連筆;1爲默認連筆,也是默認值;2在ios 上不支持。


NSString *const NSKernAttributeName;//值爲浮點數NSNumber,字距屬性,默認值爲0。


NSString *const NSStrikethroughStyleAttributeName;//值爲整型NSNumber,可取值爲


enum {


NSUnderlineStyleNone = 0×00,


NSUnderlineStyleSingle = 0×01,


};設置刪除線。


NSString *const NSUnderlineStyleAttributeName;//同上。設置下劃線。


NSString *const NSStrokeColorAttributeName;//值爲UIColor,默認值爲nil,設置的屬性同ForegroundColor。


NSString *const NSStrokeWidthAttributeName;//值爲浮點數NSNumber。設置比畫的粗細。


NSString *const NSShadowAttributeName;//值爲NSShadow,設置比畫的陰影,默認值爲nil。


NSString *const NSVerticalGlyphFormAttributeName;//值爲整型NSNumber,0爲水平排版的字,1爲垂直排版的字。string

相關文章
相關標籤/搜索