一句話筆記(04)

一句話筆記,某段時間內(nèi)遇到或看到的某個可記錄的點。 2016-8-6

  • 1、修改button 點中后的高亮狀態(tài)的顏色
  • 2、KVC 中 forKey,forKeyPath的區(qū)別
  • 3、Category 不需要引用了,可以直接使用
  • 4、UILabel 中文字居右時的留空格
1、修改button 點中后的高亮狀態(tài)的顏色

說明一下,此處不是直接修改背景顏色, 下面這個是網(wǎng)上流傳的方法,不過有時是有用的,但此處不是我的需求。

- (void)viewDidLoad {
    [super viewDidLoad];
     
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(60, 100, 200, 40)];
    [button setTitle:@"button" forState:UIControlStateNormal];
    [button setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateNormal];
    [button setBackgroundImage:[self imageWithColor:[UIColor grayColor]] forState:UIControlStateHighlighted];
    [self.view addSubview:button];
}
 
//  顏色轉(zhuǎn)換為背景圖片
- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

經(jīng)過后面我的需求,唯有通過增加圖片才得以解決:

[button setImage:[UIImage imageNamed:@"normal_pic"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"highlighted_pic"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"selected_pic"] forState:UIControlStateSelected];

當(dāng)然實際上,我這邊這個需求的理解也可以直接將點中后的高亮狀態(tài)的顏色變?yōu)闆]有:

[button setAdjustsImageWhenHighlighted:NO];

具體的還是看需求吧。

2、KVC 中 forKey,forKeyPath的區(qū)別

一般的修改一個對象的屬性的時候,forKey和forKeyPath, 沒什么區(qū)別。但是forKeyPath中可以利用運算符, 可以一層一層往下查找對象的屬性 ,能夠找到更深層的屬性。所以用forKeyPath就行了,因為它確實更強(qiáng)大一些。

3、Category 不需要引用了,可以直接使用

以前我們要使用Category 必須要頭文件引入一下,現(xiàn)在當(dāng)我們創(chuàng)建了之后,它相當(dāng)于放到我們之前的那個pch文件中,也就是全局引用了。但是往往有時候里面我們沒有寫好的情況下,一不注意就出錯了,一下子不知道錯誤在哪里,所以還是要注意一下的。

4、UILabel 中文字居右時的留空格

通常我們用UILabel 的時候,會有前面留空格或后面留空格

label.text = [NSString stringWithFormat:@"%@%@",@"  ",@"strOne"];
label.textAlignment = NSTextAlignmentLeft;

但是右邊這樣是是不管用的

label.text = [NSString stringWithFormat:@"%@%@",@"strOne",@"  "];
label.textAlignment = NSTextAlignmentRight;
不管用

\t就好啦

label.text = [NSString stringWithFormat:@"%@%@",@"strOne",@"\t"];
一個\t的效果

但是那個邊距也不是能絕對控制距離的,要空遠(yuǎn)一點我們可以多加一個\t, \t在不同型號的手機(jī)中的顯示那個距離是不一樣的, 所以要精準(zhǔn),還是得用其他辦法,自定義之類的。

常用的方法就是直接繼承UILabel, 增加一個edgeInsets 屬性,重寫

// override points. can adjust rect before calling super.
// label has default content mode of UIViewContentModeRedraw
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
- (void)drawTextInRect:(CGRect)rect;

然后設(shè)置。下面是模仿iOS-設(shè)置UILabel的內(nèi)邊距所寫的一個繼承UILabel:

#import <UIKit/UIKit.h>

@interface YSLabel : UILabel

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

@end
#import "YSLabel.h"

@implementation YSLabel

- (instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        _edgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        _edgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
}

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
    
    CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds,
                                                                 _edgeInsets) limitedToNumberOfLines:numberOfLines];
    //根據(jù)edgeInsets,修改繪制文字的bounds
    rect.origin.x -= _edgeInsets.left;
    rect.origin.y -= _edgeInsets.top;
    rect.size.width += _edgeInsets.left + _edgeInsets.right;
    rect.size.height += _edgeInsets.top + _edgeInsets.bottom;
    return rect;
}

//繪制文字
- (void)drawTextInRect:(CGRect)rect
{
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, _edgeInsets)];
}

@end

當(dāng)然此處我是先寫死設(shè)置靠右距離20的。如果需要額外設(shè)置,我們直接在外部設(shè)置edgeInsets就OK啦。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,161評論 4 61
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,603評論 30 472
  • 目錄【連載】我的燒情史(六)小淫娃研究 (七)性感往事 命運總是捉弄人。 這封信沒有如我所愿幫我找到失散18年的兒...
    李放fun閱讀 1,357評論 4 11
  • 你們有見過,固執(zhí)急躁愛生氣,而且生完氣以后還長期無法排解的人嗎?有見過這種人的生存狀態(tài)嗎?很榮幸,我身邊就...
    瑞雪吉祥閱讀 687評論 0 1

友情鏈接更多精彩內(nèi)容