iOS 常用代碼塊

不定期添加和整理

如有幫助,點(diǎn)個喜歡可好?

目錄


2.1 設(shè)置下劃線
2.2 設(shè)置顏色、字體、刪除線


內(nèi)容

<a id="1"> 1. 擴(kuò)大按鈕點(diǎn)擊范圍(擴(kuò)大點(diǎn)擊事件響應(yīng)范圍)</a>

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event //擴(kuò)大+ - 按鈕的點(diǎn)擊范圍
{
    CGPoint delePoint = [self.contentView convertPoint:point toView:self.deleBackView] ;
    CGPoint addPoint = [self.contentView convertPoint:point toView:self.addBackView];
    
    BOOL isInDeleteView = [self.deleBackView pointInside:delePoint withEvent:nil];
    BOOL isInAddView = [self.addBackView pointInside:addPoint withEvent:nil];

    if (isInDeleteView) {
        return self.deleteButton;
    }
    if (isInAddView) {
        return self.addButton;
    }
    return [super hitTest:point withEvent:event];
}

<a id="2.1"> 2.1 設(shè)置下劃線</a>

    NSMutableAttributedString *telAttr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:",ADLocalizedString(@"餐廳訂位電話")] attributes:@{NSForegroundColorAttributeName:SubTextColor77}];
    
    NSAttributedString *tel = [[NSAttributedString alloc] initWithString:branch.telenumber attributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSForegroundColorAttributeName:SubTextColor77}];
    
    [telAttr appendAttributedString:tel];
    
    [self.phoneButton setAttributedTitle:telAttr forState:(UIControlStateNormal)];

<a id="2.2"> 2.2 設(shè)置顏色、字體、刪除線</a>

 NSRange oldPriceRange = [dealInfoStr rangeOfString:activity.itemPrice];
            
 NSMutableAttributedString *dealAttrInfo = [[NSMutableAttributedString alloc] initWithString:dealInfoStr attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:15]}];
            
 [dealAttrInfo addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13], NSStrikethroughStyleAttributeName:@(NSUnderlineStyleThick)} range:oldPriceRange];
            
 dealInfo.attributedText = dealAttrInfo;

<a id="3"> 3 計算文字高度</a>

NSString *desc = [NSString stringWithFormat:@"  %@",ADLocalizedString(@"文字內(nèi)容")];
        
UIFont *textFont = [UIFont systemFontOfSize:12];
        
CGFloat height = [desc boundingRectWithSize:CGSizeMake(Main_Screen_Width - 20, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:textFont} context:nil].size.height ;

<a id="4"> 4 獲取年月日</a>

//懶加載獲取日歷
- (NSCalendar *)currentCalendar {
    if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
        return [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    }
    return [NSCalendar currentCalendar];
}
// 1.獲得年月日
        NSCalendar *calendar = [self currentCalendar];
        NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute;
        NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:lastUpdatedTime];
        NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
        
// 2.格式化日期
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        BOOL isToday = NO;
        if ([cmp1 day] == [cmp2 day]) { // 今天
            formatter.dateFormat = @" HH:mm";
            isToday = YES;
        } else if ([cmp1 year] == [cmp2 year]) { // 今年
            formatter.dateFormat = @"MM-dd HH:mm";
        } else {
            formatter.dateFormat = @"yyyy-MM-dd HH:mm";
        }
        NSString *time = [formatter stringFromDate:[NSDate date]];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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