123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- //
- // STButton.m
- // test12
- //
- // Created by apple on 15/11/14.
- // Copyright (c) 2015年 JCZ. All rights reserved.
- //
- #import "STButton.h"
- #import <UIKit/UIKit.h>
- @interface STButton()
- {
- UILabel* subLable;
- }
- @end
- @implementation STButton
- -(instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self setTitleColor:contentTextColor forState:UIControlStateNormal];
- [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
- [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
- self.titleLabel.numberOfLines = 0;
- }
- return self;
- }
- /**
- 这里Y有2中,一种是wid较大的。一种是hei较大的。
- 加入title占比。4.
- 现在的问题是,当图片小时,Y差距过大。
-
- [lastBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
- [lastBtn setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
-
- */
- /**其实有三种button。普通的。导航栏的。有子标题的。
- */
- -(CGRect)imageRectForContentRect:(CGRect)contentRect
- {
- CGFloat hei = self.frame.size.height;
- CGFloat wid = self.frame.size.width;
- CGFloat len = hei < wid ? hei:wid;
- CGFloat ivWH = len*.45;
- if (_style == 1) {
-
- return CGRectMake( (wid - ivWH)/2, hei*.1, ivWH, ivWH);
- }
- if (_style == 2) {
- return CGRectMake( 0, hei* .1, wid,hei* .6);
- }
- if (hei < wid && hei > 50) {
- ivWH = len* .3;
- return CGRectMake( (wid - ivWH)/2, hei*.2, ivWH, ivWH);
- }
- return CGRectMake( (wid - ivWH)/2, (hei - len)/2, ivWH, ivWH);
- }
- -(CGRect)titleRectForContentRect:(CGRect)contentRect
- {
- CGFloat hei = self.frame.size.height;
- CGFloat wid = self.frame.size.width;
- CGFloat len = hei < wid ? hei:wid;
- if (1 == _style) {
- return CGRectMake( 0, len * .6, wid, len*.25);
- }
- if (2 == _style) {
- return CGRectMake( 0, hei*.75, wid, hei*.25);
- }
- return CGRectMake( 0, (hei - len)/2 + len*.5, wid, len*.3);
- // return CGRectMake( 0, (hei - len)/2 + len*.6, wid, len*.2);
- }
- -(void)setSubTitle:(NSString *)subTitle
- {
- CGFloat hei = self.frame.size.height;
- CGFloat wid = self.frame.size.width;
- CGFloat len = hei < wid ? hei:wid;
- UILabel* label = [[UILabel alloc] init];
-
- [label setFrame:CGRectMake( 0, (hei - len)/2 + len*.8, wid, len*.2)];
- [label setTextAlignment:NSTextAlignmentCenter];
- [label setTextColor: contentTextColor];
- [label setText:subTitle];
- [self addSubview:label];
-
- if (self.titleLabel.font) {
- [label setFont:self.titleLabel.font];
- }else{
- [label setFont:[UIFont scaleSize:14]];
- }
- subLable = label;
- }
- - (void)setSubTitle:(NSString *)subTitle Font:(CGFloat)ft color:(UIColor *)col {
- [self setSubTitle:subTitle];
- [subLable setFont:[UIFont scaleSize:ft]];
- [subLable setTextColor:col];
- }
- @end
|