iOS常用技术-Label富文本

//
//  ViewController.m
//  Label富文本
//
//  Created by 大欢 on 16/1/19.
//  Copyright © 2016年 bjsxt. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.lineSpacing = 10;
    paragraph.paragraphSpacing = 50;
    paragraph.firstLineHeadIndent = 50;
    
    NSString * exampleString = @"根据名人慈善赛公布的名单,Drake带领的加拿大明星队将包括:NBA两届得分王麦蒂、前湖人队三冠王成员里克·福克斯、网球运动员米洛斯·劳尼克以及演员兼歌手吴亦凡。\n凯文·哈特领衔的美国明星队成员将包括:昌西·比卢普斯、“小虫”博格斯、著名主持人尼克·坎农、安东尼·安德森。";
    
    NSDictionary * dictA = @{NSFontAttributeName:[UIFont systemFontOfSize:20],
                             NSForegroundColorAttributeName:[UIColor greenColor],
                             NSBackgroundColorAttributeName:[UIColor grayColor],
                             NSParagraphStyleAttributeName:paragraph
//                             NSObliquenessAttributeName:@0.5 //斜体
//                             NSStrokeColorAttributeName:[UIColor whiteColor],
//                             NSStrokeWidthAttributeName:@2,//描边
//                             NSKernAttributeName:@20,//字间距
//                             NSStrikethroughStyleAttributeName:@2,//删除线
//                             NSUnderlineStyleAttributeName:@1,//下划线
                             };
    
    NSAttributedString * attribute = [[NSAttributedString alloc] initWithString:exampleString attributes:dictA];
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, CGRectGetWidth(self.view.bounds) - 40, 500)];
    label.attributedText = attribute;
    label.backgroundColor = [UIColor blueColor];
    label.numberOfLines = 0;
    [self.view addSubview:label]; 
}
@end
/****************************************************************/

/****************************************************************/

//
//  ViewController.m
//  Label富文本2
//
//  Created by ma c on 16/1/19.
//  Copyright © 2016年 bjsxt. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self example1];
//    [self example2];
    [self example4];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)example2 {
    NSString * string = @"@大欢,出来玩啊!";
    
    NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:string];
    
    NSRange range1 = {0, 3};
    
    NSDictionary *dict1 = @{
                           NSForegroundColorAttributeName:[UIColor blueColor],
                           NSFontAttributeName:[UIFont systemFontOfSize:25],
                           };
    
    [attributeString addAttributes:dict1 range:range1];
    
    NSDictionary *dict2 = @{
                            NSForegroundColorAttributeName:[UIColor greenColor],
                            NSFontAttributeName:[UIFont systemFontOfSize:22],
                            };
    
    NSAttributedString * string2 = [[NSAttributedString alloc] initWithString:@"好哒!" attributes:dict2];
    
    [attributeString appendAttributedString:string2];
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, CGRectGetWidth(self.view.frame)-20, 30)];
    
    label.attributedText = attributeString;
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:label];
}
/*
 使用方法:
 
 为某一范围内文字设置多个属性
 - (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
 
 为某一范围内文字添加某个属性
 - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
 
 为某一范围内文字添加多个属性
 - (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
 
 移除某范围内的某个属性
 - (void)removeAttribute:(NSString *)name range:(NSRange)range;
 
 常见的属性及说明
 NSFontAttributeName  字体
 
 NSParagraphStyleAttributeName       段落格式
 
 NSForegroundColorAttributeName     字体颜色
 
 NSBackgroundColorAttributeName    背景颜色
 
 NSStrikethroughStyleAttributeName  删除线格式
 
 NSUnderlineStyleAttributeName       下划线格式
 
 NSStrokeColorAttributeName            删除线颜色
 
 NSStrokeWidthAttributeName           删除线宽度
 
 NSShadowAttributeName                 阴影
 */
- (void)example1 {
    
    NSString * exampleString = @"乔布斯---apple";
    
    NSMutableAttributedString * mutableAttribute = [[NSMutableAttributedString alloc]initWithString:exampleString];
    
    NSRange range1 = {0, 3};
    NSRange range2 = {6, 5};
    
    [mutableAttribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50] range:range1];
    
    NSDictionary * dict = @{
                            NSForegroundColorAttributeName:[UIColor magentaColor],
                            NSBackgroundColorAttributeName:[UIColor cyanColor]
                            };
    [mutableAttribute addAttributes:dict range:range2];
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, self.view.frame.size.width-20, 60)];
    
    label.attributedText = mutableAttribute;
    label.backgroundColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
}

- (void)example3 {
    NSString * string = @"一二三四五";
    NSMutableAttributedString * mutableAttibute = [[NSMutableAttributedString alloc]initWithString:string];
    NSRange range1 = NSMakeRange(0, 3);
    [mutableAttibute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:23] range:range1];
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, CGRectGetWidth(self.view.frame), 50)];
    label.textColor = [UIColor redColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.attributedText = mutableAttibute;
    [self.view addSubview:label];
}

- (void)example4 {
    NSString * string = @"还记得你说家是唯一的城堡随着稻香河流继续奔跑微微笑 小时候的梦我知道不要哭让萤火虫带著你逃跑乡间的歌谣永远的依靠回家吧 回到最初的美好";
    NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc]init];
    paragraph.lineSpacing = 4;
    paragraph.paragraphSpacing = 7;
    paragraph.firstLineHeadIndent = 25;
    
    NSDictionary * dict = @{
                            NSFontAttributeName:[UIFont systemFontOfSize:20],
                            NSParagraphStyleAttributeName:paragraph,
                            };
    
    NSMutableAttributedString * mutableAttribute = [[NSMutableAttributedString alloc]initWithString:string attributes:dict];
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, CGRectGetWidth(self.view.frame)-20, 0)];
    label.attributedText = mutableAttribute;
    label.numberOfLines = 0;
    [label sizeToFit];
    [self.view addSubview:label];
}

@end

时间: 2024-08-05 14:54:16

iOS常用技术-Label富文本的相关文章

[iOS] 利用 NSAttributedString 进行富文本处理

/iOS /[iOS] 利用 NSAttributedString 进行富文本处理 2016年4月4日 刘小龙 iOS 许多时候我们需要以各种灵活的形式展现文本信息,即富文本.普通的 text 属性显然无法满足要求,这时我们需要利用 Foundation 中的 NSAttributedString--属性字符串进行设置.拥有文本显示功能(text 属性)的 UI 控件也都拥有 attributedText 属性. 常用方法 和 NSString 及 Foundation 框架其它集合一样,NSA

ios中label富文本的设置

1.修改不同文字和颜色 // 创建一个富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@"哈哈哈哈哈123456789"]; // 修改富文本中的不同文字的样式 [attri addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5

iOS文字编辑之富文本介绍

在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求. 今天简单介绍一下,了解到NSMuttableAttstring(带属性的字符串)和Attribute,来实现文字的不同需求. NSMuttableAttstring - 富文本文字 实例化方法和使用方法 实例化方法: 使用字符串初始化 - (id)initWithString:(NSString *)str; 例: NSMutableAttributedString *AttributedStr =

iOS学习之NSAttributedString(富文本)

NSAttributedString 叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体.字号.字体大小等各不相同的风格,还可以对段落进行格式化,一般都是对可变富文本(NSMutableAttributedString)进行一些操作 一.NSMutableAttributedString 类的部分常用方法 // 在一定范围中添加单个文字属性 // 参数1:字符属性名 // 参数2:属性值 // 参数3:范围 - (void)addAttribute:(NSStrin

常用的HTML富文本编译器UEditor、CKEditor、TinyMCE、HTMLArea、eWebEditor、KindEditor简介

1.UEditor UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于BSD协议,允许自由使用和修改代码... 主要特点: 轻量级:代码精简,加载迅速. 定制化:全新的分层理念,满足多元化的需求.采用三层架构:1. 核心层: 为命令层提供底层API,如range/selection/domUtils类.2. 命令插件层: 基于核心层开发command命令,命令之间相互独立.3. 界面层: 为命令层提供用户使用界面.满足不同层次用

iOS常用技术-计算Label高度

////  ViewController.m//  计算Label高度////  Created by 大欢 on 16/1/19.//  Copyright © 2016年 bjsxt. All rights reserved.// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {    [super vie

iOS常用技术-气泡文本自适应

////  ChatBubble.h//  ChatBubble////  Created by 大欢 on 16/1/21.//  Copyright © 2016年 bjsxt. All rights reserved.// #import <UIKit/UIKit.h> @interface ChatBubble : UIImageView //显示的文字@property (nonatomic, copy) NSString * text; @end/*****************

IOS中获取label中文本的高度及tableviewCell的自适应

1.NSString有个方法,可以获取字符串实际所占高度和宽度.可以用到UITableViewCell的自适应上在IOS7之前,用下边的 - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode; 是用来获取文本内容在指定的size内,算出所需要的实际宽度和高度.如 //这是获取字符串在指定的size内(宽度超过175,则换行)

【iOS开发-多线程】使用GCD创建多线程(iOS常用技术)

GCD 全称是Grand Central Dispatch 特点: 自动利用CPU的多核技术 自动管理线程的生命周期 使用步骤 定制任务 将任务添加队列 各类队列的特点 关于同步和异步的两种执行方式 /** * 同步方式执行任务,不管是什么队列,都不会再开一个线程 */ dispatch_sync(<#dispatch_queue_t queue#>, ^{ <#code#> }) /** * 异步方式执行任务,除了主队列都会开启一个新线程 */ dispatch_async(&l