当前位置: 首页 > news >正文

wordpress将用户锁在前台/网站排名优化软件联系方式

wordpress将用户锁在前台,网站排名优化软件联系方式,有哪些做ppt用图片的网站有哪些,合肥网站建设设计外包如何扩展UITextView以追加placeholder功能呢? 我们的需求是:追加placeholder功能 方案讨论: 通过继承UITextView的方式通过扩展UITextView的方式分析:方案1使用继承方式实现起来更简单,但是使用起来就没有那么方便&…

如何扩展UITextView以追加placeholder功能呢?

我们的需求是:追加placeholder功能

方案讨论:

  • 通过继承UITextView的方式
  • 通过扩展UITextView的方式

分析:方案1使用继承方式实现起来更简单,但是使用起来就没有那么方便;方案2 使用扩展的方式,实现起来稍比前者复杂,但是外部使用起来更简单

方案定位:采用扩展的方式,以极简的风格作为参考依据。

Tip:所谓极简,即对外接口最简,对内部可以很复杂

扩展头文件

#import <UIKit/UIKit.h>/*** @author huangyibiao** 给UITextView添加placeholder** @note 注意,此扩展有点小问题,如果在添加placeholder后,又直接赋值Text属性,则不会自动消失*       解决办法是:如果有初始值,先给text,再设置holder*/
@interface UITextView (HDFTextView)/*** 占位提示语*/
@property (nonatomic, copy)   NSString *hdf_placeholder;/*** 占位提示语的字体颜色*/
@property (nonatomic, strong) UIColor *hdf_placeholderColor;/*** 占位提示语的字体*/
@property (nonatomic, strong) UIFont  *hdf_placeholderFont;/*** 占位提示语标签*/
@property (nonatomic, strong, readonly) UILabel *hdf_placeholderLabel;@end
##说明:头文件中其实只需要公开hdf_placeholderLabel属性就可以实现了,但是依然公开了对hdf_placeholderLabel直接属性,可以根据个人习惯使用。扩展是不能扩展属性的,但是这里写成了属性的形式,其实只是利用了getter/setter方式,重写其API。内部会使用动态运行时机制来完成扩展伪属性的功能(称为伪属性是因为本质上是扩展不了属性的)

实现文件

#import "UITextView+HDFTextView.h"static const void *s_hdfTextViewPlaceholderLabelKey = "s_hdfTextViewPlaceholderLabelKey";
static const void *s_hdfTextViewPlaceholderTextKey = "s_hdfTextViewPlaceholderTextKey";@interface UIApplication (HDFTextViewHolder)@end@implementation UIApplication (HDFTextViewHolder)- (void)hdf_placehoderTextChange:(NSNotification *)nofitication {if (kIsIOS7OrLater) {return;}UITextView *textView = nofitication.object;if ([textView isKindOfClass:[UITextView class]]) {if (!kIsEmptyString(textView.text)) {textView.hdf_placeholderLabel.text = @"";} else {textView.hdf_placeholderLabel.text = textView.hdf_placeholder;}}
}@end@interface UITextView (HDFPlaceholderTextView)@property (nonatomic, strong) UILabel *placeholderLabel;@end@implementation UITextView (HDFPlaceholderTextView)- (void)setPlaceholderLabel:(UILabel *)placeholderLabel {objc_setAssociatedObject(self,s_hdfTextViewPlaceholderLabelKey,placeholderLabel,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}- (UILabel *)placeholderLabel {UILabel *label = objc_getAssociatedObject(self, s_hdfTextViewPlaceholderLabelKey);if (label == nil || ![label isKindOfClass:[UILabel class]]) {label = [[UILabel alloc] init];label.textAlignment = NSTextAlignmentLeft;label.font = self.font;label.backgroundColor = [UIColor clearColor];label.textColor = kHolderTipColor;[self addSubview:label];kWeakObject(self);self.placeholderLabel = label;CGFloat left = kIsIOS7OrLater ? 5 : 7;[label mas_makeConstraints:^(MASConstraintMaker *make) {make.edges.mas_equalTo(weakObject).insets(UIEdgeInsetsMake(7.5, left, 0, 0));}];label.enabled = NO;[kNotificationCenter addObserver:kIsIOS7OrLater ? self : [UIApplication sharedApplication]selector:@selector(hdf_placehoderTextChange:)name:UITextViewTextDidChangeNotificationobject:nil];}return label;
}@end@implementation UITextView (HDFTextView)- (void)hdf_placehoderTextChange:(NSNotification *)notification {if (kIsIOS7OrLater) {if (!kIsEmptyString(self.text)) {self.placeholderLabel.text = @"";} else {self.placeholderLabel.text = self.hdf_placeholder;}}
}- (UILabel *)hdf_placeholderLabel {return self.placeholderLabel;
}- (void)setHdf_placeholder:(NSString *)hdf_placeholder {if (kIsEmptyString(hdf_placeholder)) {objc_setAssociatedObject(self, s_hdfTextViewPlaceholderLabelKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);[self.placeholderLabel removeFromSuperview];return;}objc_setAssociatedObject(self,s_hdfTextViewPlaceholderTextKey,hdf_placeholder,OBJC_ASSOCIATION_COPY_NONATOMIC);if (!kIsEmptyString(self.text)) {self.placeholderLabel.text = @"";} else {self.placeholderLabel.text = hdf_placeholder;}
}- (NSString *)hdf_placeholder {return objc_getAssociatedObject(self, s_hdfTextViewPlaceholderTextKey);
}- (void)setHdf_placeholderColor:(UIColor *)hdf_placeholderColor {self.placeholderLabel.textColor = hdf_placeholderColor;
}- (UIColor *)hdf_placeholderColor {return self.placeholderLabel.textColor;
}- (void)setHdf_placeholderFont:(UIFont *)hdf_placeholderFont {self.placeholderLabel.font = hdf_placeholderFont;
}- (UIFont *)hdf_placeholderFont {return self.placeholderLabel.font;
}@end
说明:这里的实现文件中使用了运行时机制(runtime)来实现,这里对文本改变的监听,交给了自己和UIApplication,是为了兼容到IOS6.0,在6.0下,交给自己是不可行的,会崩溃,因此移交给UIApplication单例对象来管理。

############提示:这里使用了Masonary这个自动布局的三方库,让这个placeholderlabel自动根据uitextview的大小变化而变化。代码中使用了判断IOS系统,判断是否为空串的代码,这里不写出来了,自己替掉即可。

下面就是使用了,使用起来就很简单了

  self.remarkView.text = @"哈哈,很简单吧";self.remarkView.hdf_placeholder = @"写点什么...";

备注:当需求中需要给textview添加placeholder时,网上的都是继承的方式,还需要修改很多地方,这是不可行的,所以我才提出这个问题来,然后做出了自己的解决方案。如果对大家有用,请放心地拿去用吧。

兼容IOS6.0及其以上版本
http://www.lbrq.cn/news/1029349.html

相关文章:

  • 宝鸡市住房和城市建设局网站/新闻发稿软文推广
  • 新万网站建设/百度关键词优化平台
  • 个人域名网站可以做企业站吗/深圳关键词排名优化系统
  • 新网站建设运营年计划书/西安疫情最新通知
  • 做网站都需要什么工具/福建优化seo
  • 揭阳手机网站建设/青岛seo推广公司
  • 设计网站推荐html/找网站设计公司
  • 如何做网站跳转/便宜的seo官网优化
  • 国家开发银行助学贷款网站/seo优化报告
  • 建一个app和网站那个比较好/公司官网模板
  • 传业做微采商城网站/智能建站模板
  • 池州网站优化/搜索历史记录
  • 网站如何做百度权重/常见的网络推广方式包括
  • 网站建设的基本流程是什么/免费网站建设seo
  • 帆布网站做哪个/百度官方网页版
  • 网站使用网络图片做素材 侵权吗/安徽疫情最新情况
  • 科技 杭州 网站建设/新闻稿件
  • 方城微网站开发/10种营销方法
  • 图片演示dw做网站/谷歌chrome安卓版
  • 政府网站建设如何更好服务人民/互联网产品营销策划方案
  • 成都最新疫情最新轨迹公布/搜索引擎优化的主要特征
  • 点网站出图片怎么做/产品如何做市场推广
  • 国内做批发的网站/西安网络推广seo0515
  • tp框架做餐饮网站/怎样推广自己的app
  • 网站源码系统/百度搜索排名服务
  • 企业定制网站建设公司/百度客服人工服务电话
  • 网站建设如何搞活动/香港服务器
  • 找公司做网站多少钱成都/网络服务中心
  • 网站开发价格 北京/今日新闻摘抄十条
  • 凤台县城乡建设委员会网站/天猫seo搜索优化
  • ZigBee入门与提高(3)—— ZigBee协议初识
  • 基于FPGA的8PSK+卷积编码Viterbi译码通信系统,包含帧同步,信道,误码统计,可设置SNR
  • 记某一次仿真渗透测试
  • 《Python学习之基础语法2:掌握程序流程控制的艺术》
  • 串口通信学习
  • ncurses 6.5 交叉编译移植到OpenHarmomy