O~2程序员iOS 程序员

iOS 常用代码块

2016-08-26  本文已影响319人  yzhxcql

不定期添加和整理

如有帮助,点个喜欢可好?

目录


2.1 设置下划线
2.2 设置颜色、字体、删除线


内容

<a id="1"> 1. 扩大按钮点击范围(扩大点击事件响应范围)</a>

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event //扩大+ - 按钮的点击范围
{
    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 设置下划线</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 设置颜色、字体、删除线</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(@"文字内容")];
        
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]];
上一篇下一篇

猜你喜欢

热点阅读