iOS文本布局探讨之三——使用TextKit框架进行富文本布局 一、引言 关于图文混排,其实以前的博客已经讨论很多,在实际开发中,经常使用第三方的框架来完成排版的需求,其中RCLabel...二、原生UILabel真的只能渲染文字么? CoreText是一个比较底层且十分强大的文本渲染框架,但是其使用起来并不是十分方便。在较低版本的iOS系统中,要进行富文本排版十分困难。...在iOS6中,系统为UILabel,UITextView等这类文本渲染控件引入了NSAttributedString属性,有了NSAttributedString这个类,创建灵活多彩的文本控件变得十分轻松...:[NSAttributedString attributedStringWithAttachment:attach2] atIndex:130]; UILabel * label = [[UILabel...三、为富文本附件添加用户交互能力 TextKit框架强大到只使用UILabel就可以完成复杂的富文本布局,但是UILabel有一个致命的缺陷,其无法进行用户交互。
在使用中通过将AttributedString赋值给控件的 attributedText 属性来添加文字样式。 可设置的控件有UILabel、UITextField和UITextView。...:attr4]; 设置删除线 /*!...文字的图文混排\n"; NSTextAttachment *textAttachment = [[NSTextAttachment alloc]init]; textAttachment.image = [UIImage...使用方式同删除线一样。...之所以把 NSLinkAttributeName 属性单独列出来,是因为在 UILabel 和 UITextField 中是无法使用该属性的。
NSTextAttachment 需求:图文混排 初始实现的代码如下: let label = UILabel() label.frame = CGRect(x: 50.0, y: 150.0, width...= 0 self.view.addSubview(label) let attributedStr = NSMutableAttributedString() // 图片 let image: UIImage...= UIImage(named: "online") ??...UIImage() // 图片高度跟文字高度一致 let imageHeight = label.font.lineHeight // 高度确定后,根据宽高比,算出图片应该显示的高度 let imageWidth...(attachment: attach) attributedStr.append(imageStr) // 增加图片后与文字间距 let spacingStr = NSAttributedString
let attributedText = NSAttributedString(string: "道之不存,师之所存也", attributes: [NSStrikethroughStyleAttributeName
UIButton控件是应用界面中常用的一个控件,用法总结: 一、初始化 UIButton的初始化一般使用其类方法,+ (id)buttonWithType:(UIButtonType)buttonType... forState:(UIControlState)state; //设置按钮背景图片和显示图片时的状态 - (void)setAttributedTitle:(NSAttributedString *... (NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0); 五、一些只读属性...,readonly,retain) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0); //这两个参数需要注意,虽然他们是只读属性不能重新设置...,但是我们可以设置label和imageView的相关属性 @property(nonatomic,readonly,retain) UILabel *titleLabel NS_AVAILABLE_IOS
UIColor对象,默认值为nil, 透明色 NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符...设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似 NSUnderlineColorAttributeName...)range; - (void)setAttributedString:(NSAttributedString *)attrString; - (void)beginEditing; - (void)...需要创建另一个富文本,带图片的) NSTextAttachment *attach = [[NSTextAttachment alloc]init]; attach.image = [UIImage...appendAttributedString:imageStr]; //7.创建Label来显示富文本 UILabel *myLabel = [[UILabel alloc]
xib中想要让label的文字换行时,需要使用option+enter组合键进行换行,使用"\n"是不管用的。...快速登录按钮的布局 首先快速登录方便的两条线都是图片,只需要简单设置frame即可,下面的三个快速登录明显是按钮,但是我们知道,UIButton默认的是UIImage在左边,titleLabel在右边,...那么我们需要使默认的布局改为UIImage在上面,titleLabel在下面。...,下面我们来看一些使用的实例 图文混排 UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(100, 100, 200,...:second]; // 3 - 图片后部分 NSAttributedString *third = [[NSAttributedString alloc] initWithString:@"哈哈哈"
:UIControlStateNormal]; //设置前景图片 前景图片必须是镂空图,或者是线条勾勒的图片 [button setImage:[UIImage imageNamed:@"7.png"...UI_APPEARANCE_SELECTOR; // default is nil 设置button 某个状态下的富文本标题 - (void)setAttributedTitle:(nullable NSAttributedString...:(UIControlState)state; 返回button 某个状态下的富文本标题 - (nullable NSAttributedString *)attributedTitleForState...; 获取按钮当前标题富文本 @property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle...@property(nullable, nonatomic,readonly,strong) UILabel *titleLabel NS_AVAILABLE_IOS(3_0); @property
往数据库里写保存文件路径的时候,不要写全路径,因为软件更新或者重新安装沙盒路径会变 更新的流程是这样的:更新时,先在新的路径里安装新程序,然后把旧程序文件夹里的配置文件之类的文件拷贝到新的路径里去,然后删除旧程序...oldString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //若要只删除...NSParagraphStyleAttributeName:textStyle}]; 设置粗体文字 首先可以上这个网站:http://iosfonts.com/查看自己要用的字体是否支持粗体,然后使用下面方法...underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}; myLabel.attributedText = [[NSAttributedString...defaultCStringEncoding]]; [nsArray addObject:item]; } return ansArray; NSArray添加CGPoint对象 一般使用
它良好的结合了 UIKit 和 Core Graphics/Quartz: UIKit 的 UILabel 允许你通过在 IB 中简单的拖曳添加文本,但你不能改变文本的颜色和其中的单词。...UITextView,UITextField、UILabel都已经基于Text Kit重新构建,所以它们都支持分页文本、文本包装、富文本编辑、交互式文本着色、文本折叠和自定义截取等特性。...CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, (__bridge void *)imgInfoDic); // 使用...UIColor对象,默认值为nil, 透明色 NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符...四、TextKit实例 1、添加图片-NSTextAttachment NSTextAttachment用于添加图片 //添加表情 UIImage * image = [UIImage imageNamed
2.用UILabel和UITextField实现输入框。...[strongSelf.superView removeFromSuperview]; strongSelf.superView = nil; }]; } //输入密码或删除时...else { label.text = @""; } } } //删除时...else { for (int i = 0; i < PasswordBoxNumber; i++) { UILabel *label...self.completionBlock(self.textField.text); } } } demo下载地址:https://github.com/dt8888/PursePossword 三.demo 的使用
图文混排在这两块中使用最为常见,我已经做好了demo:图文混排demo。 文中会讲述几点小技巧:图文混排、动态计算文字长度、图片拉伸方法。...以前的做法 在以前做图文混排的时候,经常使用OHAttributedLabel,后来苹果吸取了一些第三方的优点,对NSString做了扩展,作者也不再更新,推荐用系统的方法来实现图文混排。...先上效果图,聊天界面中的图文混排: 要实现这样的效果,code4app上似乎有很多种做法,还有一些奇葩的一个字符一个label,但是今天要讲述的做法,是目前为止我看到的最简单的做法了,只用一个UILabel...,需要用到UILabel的attributedText属性。...NSTextAttachment*textAttachment = [[NSTextAttachment alloc] init]; //给附件添加图片 textAttachment.image= [UIImage
而NSMutableAttributedString有可以最自己内容作出颜色以及大小的调整,这样结合起来使用的话,就远比UILabel 灵活的多,效果也酷炫的多,也许CATextLayer就是为了NSMutableAttributedString...下面就简要介绍下CATextLayer 的常规使用,不足之处,还望朋友们下面留言补充,不胜感谢。...在使用中通过将AttributedString赋值给控件的 attributedText 属性来添加文字样式。有属性的控件有UILabel、UITextField和UITextView。...appendAttributedString:attr1]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(60, 100,...NSUnderlineStyleAttributeName 下划线 1或0 //NSUnderlineColorAttributeName 下划线颜色 //NSStrikethroughStyleAttributeName 删除线
灵活使用NSAttributedString可以更轻松的创建出内容复杂的标签。需要注意一点:如果一个label设置了这个属性,那它其他的设置都将失效。...NSString *)str attributes:(NSDictionary *)attrs; //通过自身对象初始化 //- (instancetype)initWithAttributedString:(NSAttributedString... range:NSMakeRange(0, 4)]; 通过测试,发现上面有些键值并没有作用,可以替换下面的方法,效果相同,不同的地方在于其传值的类型不同,下面的方法更加方便(使用
之前选中的按钮 @property(nonatomic, retain) UIButton *selectedBtn; @end 然后开始写自己想要的东西了,在viewDidLoad中: (1)删除继承父类而来的...即标签和标签下的标题 (3)然后循环给4个按钮添加点击事件,触发事件是同一个clickBtn - (void)viewDidLoad { [super viewDidLoad]; //删除现有的...主页发现按钮 UIButton *findBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [findBtn setImage:[UIImage...imageNamed:@"icon_home_n"] forState:UIControlStateNormal]; [findBtn setImage:[UIImage imageNamed...//跳转相应的视图控制器 self.selectedIndex = sender.tag - 1; } 然后就基本实现了一个视图控制器的功能了,自己还有什么需求可以往里加,使用就跟视图的标签视图一样使用就行
UIView 可以直接在 storyboard 里面拖拽使用,也可以使用纯代码方式使用。 UILabel、UITextField、UIButton UILabel 显示静态文本。...NSAttributedString textField.attributedPlaceholder = NSAttributedString(string: "请输入信息", attributes:...@IBOutlet var product: UILabel! @IBOutlet var flower: UIImageView!...image = UIImage(named: "setting_about_pic") return cell! } } 数据源 数据不再固定,而是由外界提供,多使用数组。...image = UIImage(named: "iPhone") return cell! } } 静态单元格 需要使用 UITableViewController。
所以按照UILabel.layer默认的坐标(0, 0),在左上角进行了绘制。 接着,我们尝试使用CoreText来渲染一段文本。...用我们在Windows常用的画图来参考,当我们使用画笔在白板中写字时,图形上下文就是画笔的属性设置、白板大小、画笔位置等等。...通常我们都会使用UIKit进行渲染,所以iOS系统在drawRect返回CGContext的时候,默认帮我们进行了一次变换,以方便开发者直接用UIKit坐标系进行渲染。...这时我们产生新的困扰: 用CoreText渲染文字的上下颠倒现象解决,但是修改后的坐标系UIKit无法正常使用,如何兼容两种坐标系?...iOS可以使用CGContextSaveGState()方法暂存context状态,然后在CoreText绘制完后通过CGContextRestoreGState ()可以恢复context的变换。
内存分析 ( ① 使用 UIImage imageNamed:图片名称 加载图片 | ② 使用 UIImage imageWithContentsOfFile:Bundle路径名 加载图片 ) ( 3...]; 3.控件类型准备 : 父容器中添加 UIButton 组件 , 用于区分 UILabel 和 UIButton , 这里只删除 UILabel 控件 ; 4.判定组件类型 : 使用 isKindOfClass...判断 对象 类型 ; [view isKindOfClass:[UIButton class]] 5.删除所有 UILabel 控件 : 使用 for in 循环 删除所有的 UILabel 控件 ;...使用 UIImage imageWithContentsOfFile:Bundle路径名 加载图片 ) UIImage 内存分析 : 1.使用 UIImage imageNamed:图片名称 加载图片...[UIImage imageNamed:@"1.png"]; 2.使用 UIImage imageWithContentsOfFile:Bundle路径名 加载图片 : 使用该方式加载图片时 , 如果图片没有被引用时
否则会影响美观 self.tableView.tableFooterView = [UIView new]; } 满足代理方法,可以分别配置,都是可选的 空白页图片 - (UIImage...*)imageForEmptyDataSet:(UIScrollView *)scrollView { return [UIImage imageNamed:@"empty_placeholder...alloc] initWithString:text attributes:attributes]; } - (NSAttributedString *)descriptionForEmptyDataSet...alloc] initWithString:@"Continue" attributes:attributes]; } - (UIImage *)buttonImageForEmptyDataSet...:(UIScrollView *)scrollView forState:(UIControlState)state { return [UIImage imageNamed:@"button_image
下面就是具体的实现代码: /// 使用此方法时请标明源作者:欧阳大哥2013。本方法符合MIT协议规范。.../// @param text 要计算的简单文本NSString或者属性字符串NSAttributedString对象 /// @param numberOfLines 指定最大显示的行数,如果为0则表示不限制最大行数.../// @param font 指定计算时文本的字体,可以为nil表示使用UILabel控件的默认17号字体 /// @param textAlignment 指定文本对齐方式默认是NSTextAlignmentNatural...*originAttributedString = (NSAttributedString *)text; //对于属性字符串总是加上默认的字体和段落信息。...UILabel *label = [UILabel new]; label.text = text; label.numberOfLines = rand() % 100