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

静态网站设计模板添加书签网站代码

静态网站设计模板,添加书签网站代码,创建全国文明城市英语作文,石碣网站建设自定义按钮UIControl 写在前面 #图标和文字在一起是开发难免的问题#xff0c;系统的按钮默认是图片居左的文字居右的#xff0c; 且图片和文字的距离不好调整#xff0c;图片的位置更是让人头疼#xff0c; 故在闲暇之余封装了一个控件。 复制代码所用到知识的 # 1、苹果系…自定义按钮UIControl 写在前面 #图标和文字在一起是开发难免的问题系统的按钮默认是图片居左的文字居右的 且图片和文字的距离不好调整图片的位置更是让人头疼 故在闲暇之余封装了一个控件。 复制代码所用到知识的 # 1、苹果系统自带的自动布局减少第三方的依赖 # 2、kvo监听UIControl的状态 # 3、富文本的使用如何更新约束如何获取一段文字的宽高复制代码先看看效果 拥有功能 1、任意调整图片和文字的间距。 2、图片的位置可以放置在上下左右不同位置。 3、支持富文本自定义布局高亮和选中状态。 复制代码如何使用 /**自定义按钮初始化方法param image 默认的图片param title 标题param titleFond 标题大小param imageMargin 标题与图标的距离param imageAlignmentTYpe 图片的显示类型return 自定义按钮的实例*/ - (instancetype)initWithImage:(UIImage *) imagetitle:(NSString *) titletitleFond:(UIFont *) titleFondimageMargin:(CGFloat) imageMarginimageAlignmentTYpe:(MyButtonImageAlignmentTYpe )imageAlignmentTYpe;_myButton [[MyButton alloc]initWithImage:[UIImage imageNamed:cache_pause]title:来点我啊titleFond:[UIFont systemFontOfSize:14]imageMargin:10imageAlignmentTYpe:MyButtonImageAlignmentLeft];[self.view addSubview:_myButton];self.myButton.frame CGRectMake(20, 100, 120, 40);self.myButton.backgroundColor UIColor.grayColor;[self.myButton setImage:[UIImage imageNamed:cache_delete] withState:UIControlStateSelected];[self.myButton setImage:[UIImage imageNamed:cache_pause] withState:UIControlStateHighlighted];[self.myButton setTitle:选中了 withState:UIControlStateSelected];[self.myButton setTitle:正在按下... withState:UIControlStateHighlighted];[self.myButton setTitleColor:UIColor.blueColor withState:UIControlStateSelected];[self.myButton setTitleColor:UIColor.yellowColor withState:UIControlStateHighlighted];这样就可以了和普通的按钮用法一样。复制代码关键代码 1、自定义布局只列出了一种 //图片居左 - (void)setImageLeftLayoutConstraint {CGFloat imageWidth self.normalImage.size.width;CGFloat titleWidth [self sizeWithText:self.titleLabel.text font:self.titleLabel.font].width;CGFloat imageCenterXMargin (imageWidth /2.0 - (imageWidth titleWidth self.imageMargin) / 2.0);//创建Image约束NSLayoutConstraint *imageCenterYLc [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];NSLayoutConstraint *imageCenterXLc [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:imageCenterXMargin];[self addConstraints:[imageCenterYLc,imageCenterXLc]];//创建title约束NSLayoutConstraint *titleCenterYLc [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];NSLayoutConstraint *titleLeftLc [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.imageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:self.imageMargin];[self addConstraints:[titleCenterYLc,titleLeftLc]]; }//获取文字的宽高 - (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font {NSDictionary *attrs {NSFontAttributeName : font};return [text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; }//kvo监听 - (void)addObserver {[self addObserver:selfforKeyPath:enabledoptions: NSKeyValueObservingOptionNewcontext:nil];[self addObserver:selfforKeyPath:selectedoptions: NSKeyValueObservingOptionNewcontext:nil];[self addObserver:selfforKeyPath:highlightedoptions: NSKeyValueObservingOptionNewcontext:nil];[self.titleLabel addObserver:selfforKeyPath:textoptions:NSKeyValueObservingOptionNewcontext:nil]; }//kvo 监听处理只列出了部分 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionaryNSKeyValueChangeKey,id *)change context:(void *)context {if ([keyPath isEqualToString:selected]) {if(self.selected) {self.imageView.image self.selectedImage ? self.selectedImage : self.normalImage;self.backgroundColor self.selectedBackgroundColor ? self.selectedBackgroundColor : self.normalBackgroundColor;if (!self.selectedTitleAttribute !self.normalTitleAttribute) {self.titleLabel.text self.selectedTitle ? self.selectedTitle : self.normalTitle;self.titleLabel.textColor self.selectedTitleColor ? self.selectedTitleColor : self.normalTitleColor;return;}self.titleLabel.attributedText self.selectedTitleAttribute ? self.selectedTitleAttribute : self.normalTitleAttribute;} else {self.imageView.image self.normalImage;self.backgroundColor self.normalBackgroundColor;if (!self.normalTitleAttribute) {self.titleLabel.text self.normalTitle;self.titleLabel.textColor self.normalTitleColor;return;}self.titleLabel.attributedText self.normalTitleAttribute;}} //监听到字体变化更新自动布局if ([keyPath isEqualToString:text]) {[self removeConstraints:self.constraints];[self.superview layoutIfNeeded];[self updateConstraints:self.imageAlignmentTYpe];[self.superview layoutIfNeeded];} 复制代码以上只是部分代码详细代码请查看 欢迎查看MyApp 自定义按钮 https://github.com/dudongge/MyApp 如果对您有帮助请不吝star一下
http://www.zqtcl.cn/news/290499/

相关文章:

  • 注册网站引流wordpress5.0.2图集怎么发布
  • 360产品展示网站哈尔滨个人建站模板
  • 怎么做网站的浏览量陕西省住房和建设厅官方网站
  • 上海网站 备案查询平面设计接单网站有哪些
  • 用别人的公司名字做网站想自己做网站推广
  • 百度智能建站平台建设工程信息网官网入口查询
  • 比价网站源码整站程序服务器怎么发布网站
  • html插件代码大全济南网站关键词优化公司
  • 优秀的手机网站设计网站推广的特点
  • 滨州北京网站建设电子商务网站规划与管理
  • 如何注册公司网站域名中国有几大网站
  • php网站留言板怎么做wordpress 相关文章推荐
  • 怎么看网站被惩罚专业的网站建设流程
  • 如何制作手机免费网站模板下载用户体验设计师是什么
  • php网站建设基本流程基于php网站开发设计
  • 建设一个网站需要哪些软硬件条件无做弊的棋牌游戏网站
  • 最有设计感的网站扬中网站建设 优帮云
  • 企业建设银行网站登录不了wordpress需要ftp
  • 广州营销型网站建设团队专业建设内涵包括哪些内容
  • 网站如何做响应式布局外国网站上做Task
  • 知乎网站建设入门书大渡口集团网站建设
  • 免费网站建设是什么宁波网站建设的价格表
  • 网站设计导航栏高度网站设计的经营范围
  • 帮别人建设网站多少利润北京网站建设公司华网制作作
  • 微信网站需要备案吗瑞安商业网站建设
  • 做网站如何计算工资wordpress stheme
  • 网站建设销售人才简历wordpress 搜索tag
  • 设计网站专业云南旅行社网站开发
  • 小规模开普票网站建设几个点张浦专业做网站
  • 点击图片跳转到网站怎么做链接网址后缀名大全