特别实用而且功能强大的attributedText属性
来源:互联网 发布:swfobject.js demo 编辑:程序博客网 时间:2024/05/17 03:56
#import "ViewController.h"#import <CoreText/CoreText.h>@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad];//设置字体 UIButton *btn=[[UIButton alloc]init]; NSMutableDictionary *btndict=[NSMutableDictionary dictionary]; btndict[NSFontAttributeName]=[UIFont systemFontOfSize:15]; [btn setTitleTextAttributes:btndict forState:UIControlStateNormal];//设置删除线 UITextField *textfield=[[UITextField alloc]init]; textfield.text=@"试试"; NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:textfield.text]; [attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, textfield.text.length)]; [textfield setAttributedText:attrString]; 1.控件 UIView UILabel UITextField UITextView UIButton 2.字体、大小、单位、颜色 */ UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 260)]; label.text = @"Label Text Content, This is a text label things attribute";//默认为空 label.font = [UIFont systemFontOfSize:17];//默认使用系统的17 label.textColor = [UIColor orangeColor];//默认使用文本黑色 label.shadowColor = [UIColor lightGrayColor];//默认没有阴影 label.shadowOffset = CGSizeMake(1,0);//默认是一个向上的阴影(0,-1) label.textAlignment = NSTextAlignmentCenter;//默认是左对齐 label.lineBreakMode = NSLineBreakByTruncatingTail;//段落样式,默认是最后截断尾巴,用...代替 //富文本的基本数据类型,属性字符串。以此为基础,如果这个设置了相应的属性,则会忽略上面设置的属性,默认为空 NSString *string = label.text; const CGFloat fontSize = 16.0; NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string]; NSUInteger length = [string length]; //设置字体 UIFont *baseFont = [UIFont systemFontOfSize:fontSize]; [attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];//设置所有的字体 UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize]; [attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];//设置Text这四个字母的字体为粗体 //设置倾斜,需要导入coreText UIFont *italicFont = GetVariationOfFontWithTrait(baseFont, kCTFontTraitItalic); [attrString addAttribute:NSFontAttributeName value:italicFont range:[string rangeOfString:@"Label"]]; // 设置颜色 UIColor *color = [UIColor redColor]; [attrString addAttribute:NSForegroundColorAttributeName value:color range:[string rangeOfString:@"Content"]]; [attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"ent"]]; //可以对这些属性设置值 //字体名称有以下:// label.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:24]; [attrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-BoldItalic" size:18] range:[string rangeOfString:@"Label"]]; label.numberOfLines = 2; NSMutableParagraphStyle * style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; style.lineSpacing = 10;//增加行高 style.headIndent = 10;//头部缩进,相当于左padding style.tailIndent = -10;//相当于右padding style.lineHeightMultiple = 1.5;//行间距是多少倍 style.alignment = NSTextAlignmentLeft;//对齐方式 style.firstLineHeadIndent = 20;//首行头缩进 style.paragraphSpacing = 10;//段落后面的间距 style.paragraphSpacingBefore = 20;//段落之前的间距 [attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, length)]; [attrString addAttribute:NSKernAttributeName value:@2 range:NSMakeRange(0, length)];//字符间距 2pt [attrString addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"is"]];//设置文字描边颜色,需要和NSStrokeWidthAttributeName设置描边宽度,这样就能使文字空心 [attrString addAttribute:NSStrokeWidthAttributeName value:@2 range:[string rangeOfString:@"is"]];//空心字,文字边框描述 [attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:[string rangeOfString:@"text"]];//下划线 [attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleThick) range:[string rangeOfString:@"label"]];//厚的下划线 [attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:[string rangeOfString:@"things"]];//删除线 [attrString addAttribute:NSStrikethroughColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"things"]];//删除线蓝色 label.attributedText = attrString; label.highlightedTextColor = [UIColor redColor];//设置文本高亮显示颜色,与highlighted一起使用。 label.highlighted = NO; //高亮状态是否打开 label.enabled = YES;//设置文字内容是否可变 label.userInteractionEnabled = YES;//设置标签是否忽略或移除用户交互。默认为NO label.baselineAdjustment = UIBaselineAdjustmentNone;//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为。// UIBaselineAdjustmentAlignBaselines=0,默认,文本最上端与中线对齐。// UIBaselineAdjustmentAlignCenters, 文本中线与label中线对齐。// UIBaselineAdjustmentNone, 文本最低端与label中线对齐。; [self.view addSubview:label]; } UIFont * GetVariationOfFontWithTrait(UIFont *baseFont, CTFontSymbolicTraits trait) { CGFloat fontSize = [baseFont pointSize]; CFStringRef baseFontName = (__bridge CFStringRef)[baseFont fontName]; CTFontRef baseCTFont = CTFontCreateWithName(baseFontName, fontSize, NULL); CTFontRef ctFont = CTFontCreateCopyWithSymbolicTraits(baseCTFont, 0, NULL, trait, trait); NSString *variantFontName = CFBridgingRelease(CTFontCopyName(ctFont, kCTFontPostScriptNameKey)); UIFont *variantFont = [UIFont fontWithName:variantFontName size:fontSize]; CFRelease(ctFont); CFRelease(baseCTFont); return variantFont;};- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
0 0
- 特别实用而且功能强大的attributedText属性
- UILable的attributedText使用
- UILabel 自适应的text属性和attributedText搜索属性有什么区别
- label中的attributedText属性,很强大的设置,以前用的弱爆了
- label中的attributedText属性,很强大的设置,以前用的弱爆了
- UILabel attributedText的一个坑
- UILabel中attributedText的使用
- 特别实用的ajax 请求方法
- net时代winforms的地位特别尴尬,而且会一直尴尬下去? ---- 之我见
- 一个带有属性的字符串(比如文字颜色/字体属性等文字属性) 在label上显示需要使用 attributedText 属性
- listview的几个特别的属性设置
- CSS样式有哪些常用的属性?功能强大!
- android ListView几个比较特别的属性
- android ListView几个比较特别的属性
- android ListView几个比较特别的属性
- Android ListView几个比较特别的属性
- Android-ListView几个比较特别的属性
- Android之UI- listview特别的属性
- 1、@Transient @[email protected]�射,ORM框架将忽略该属性; 如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则ORM框架默认其注解为@B
- 动态规划之线性动归
- 国内投资者投资港股的四种方法和港股必知25件事
- 微信红包(腾讯2016研发工程师编程题)
- Fiddler可以抓取https的请求
- 特别实用而且功能强大的attributedText属性
- 同一Activity的实例被多次重复创建
- @value注解使用的简单介绍 ---谷营中西
- ViewTreeObserver简介
- Codeforces Round #357 (Div. 2) A Good Contest(水题)
- 特征哈希(Feature Hashing)
- springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制
- Qt之MVC使用
- pixhawk资料总结(随时更新)