typedef NS_ENUM(NSUInteger, HXButtonEdgeInsetsStyle) {
HXButtonEdgeInsetsStyleTop,// image在上,label在下
HXButtonEdgeInsetsStyleLeft,// image在左,label在右
HXButtonEdgeInsetsStyleBottom,// image在下,label在上
HXButtonEdgeInsetsStyleRight// image在右,label在左
};
@interfaceUIButton(ImageTitleSpacing)
/**
* 设置button的titleLabel和imageView的布局样式,及间距
*
* @param styletitleLabel和imageView的布局样式
* @param spacetitleLabel和imageView的间距
*/
- (void)layoutButtonWithEdgeInsetsStyle:(HXButtonEdgeInsetsStyle)style
imageTitleSpace:(CGFloat)space;
#pragma mark - ImageUpTitleDown
@property (nonatomic) IBInspectable BOOL hx_imageUpTitleDown;// Defaults space 10.
@end
#import "UIButton+ImageTitleSpacing.h"
@implementationUIButton(ImageTitleSpacing)
#pragma mark - ImageUpTitleDown
- (BOOL)hx_imageUpTitleDown
{
self.hx_imageUpTitleDown = YES;
return YES;
}
- (void)setHx_imageUpTitleDown:(BOOL)hx_imageUpTitleDown
{
if(hx_imageUpTitleDown) {
[self layoutButtonWithEdgeInsetsStyle:HXButtonEdgeInsetsStyleTop imageTitleSpace:10.0];
}
}
- (void)layoutButtonWithEdgeInsetsStyle:(HXButtonEdgeInsetsStyle)style
imageTitleSpace:(CGFloat)space
{
// 1. 得到imageView和titleLabel的宽、高
CGFloat imageWith = self.imageView.frame.size.width;
CGFloat imageHeight = self.imageView.frame.size.height;
CGFloatlabelWidth =0.0;
CGFloatlabelHeight =0.0;
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
// 由于iOS8中titleLabel的size为0,用下面的这种设置
labelWidth =self.titleLabel.intrinsicContentSize.width;
labelHeight =self.titleLabel.intrinsicContentSize.height;
}else{
labelWidth =self.titleLabel.frame.size.width;
labelHeight =self.titleLabel.frame.size.height;
}
// 2. 声明全局的imageEdgeInsets和labelEdgeInsets
UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
// 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
switch(style) {
case HXButtonEdgeInsetsStyleTop:
{
imageEdgeInsets =UIEdgeInsetsMake(-labelHeight-space/2.0,0,0, -labelWidth);
labelEdgeInsets =UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0,0);
}
break;
case HXButtonEdgeInsetsStyleLeft:
{
imageEdgeInsets =UIEdgeInsetsMake(0, -space/2.0,0, space/2.0);
labelEdgeInsets =UIEdgeInsetsMake(0, space/2.0,0, -space/2.0);
}
break;
case HXButtonEdgeInsetsStyleBottom:
{
imageEdgeInsets =UIEdgeInsetsMake(0,0, -labelHeight-space/2.0, -labelWidth);
labelEdgeInsets =UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith,0,0);
}
break;
case HXButtonEdgeInsetsStyleRight:
{
imageEdgeInsets =UIEdgeInsetsMake(0, labelWidth+space/2.0,0, -labelWidth-space/2.0);
labelEdgeInsets =UIEdgeInsetsMake(0, -imageWith-space/2.0,0, imageWith+space/2.0);
}
break;
default:
break;
}
// 4. 赋值
self.titleEdgeInsets= labelEdgeInsets;
self.imageEdgeInsets= imageEdgeInsets;
}
@end
#import
@interfaceUILabel(FormattedText)
- (void)setTextColor:(UIColor*)textColorrange:(NSRange)range;
- (void)setFont:(UIFont*)fontrange:(NSRange)range;
- (void)setTextColor:(UIColor *)textColor beforeOccurenceOfString:(NSString*)separator;
- (void)setTextColor:(UIColor *)textColor afterOccurenceOfString:(NSString*)separator;
- (void)setFont:(UIFont *)font beforeOccurenceOfString:(NSString*)separator;
- (void)setFont:(UIFont *)font afterOccurenceOfString:(NSString*)separator;
- (void)setTextUnderLine:(UIColor*)lineColorrange:(NSRange)range;
- (void)setTextWithoutUnderLineInRange:(NSRange)range;
- (void)setTextColor:(UIColor*)textColorString:(NSString*)searchString;
- (void)setTextColor:(UIColor*)textColorString:(NSString*)searchStringunderLineColor:(UIColor*)underlineColor;
- (void)setTextColor:(UIColor *)textColor fromOccurenceOfString:(NSString*)separator1 toOccurenceOfString:(NSString*)separator2;
@end
#import "UILabel+FormattedText.h"
@implementationUILabel(FormattedText)
- (void)setTextColor:(UIColor*)textColorrange:(NSRange)range
{
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: self.attributedText];
[textaddAttribute: NSForegroundColorAttributeName
value: textColor
range: range];
[self setAttributedText: text];
}
- (void)setFont:(UIFont*)fontrange:(NSRange)range
{
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: self.attributedText];
[textaddAttribute: NSFontAttributeName
value: font
range: range];
[self setAttributedText: text];
}
- (void)setTextColor:(UIColor *)textColor beforeOccurenceOfString:(NSString*)separator{
NSRangerange = [self.textrangeOfString:separator];
if (range.location != NSNotFound)
{
range.length= range.location;
range.location=0;
[selfsetTextColor:textColorrange:range];
}
}
- (void)setTextColor:(UIColor *)textColor afterOccurenceOfString:(NSString*)separator{
NSRangerange = [self.textrangeOfString:separator];
if (range.location != NSNotFound)
{
range.length=self.text.length- range.location;
[selfsetTextColor:textColorrange:range];
}
}
- (void)setFont:(UIFont *)font beforeOccurenceOfString:(NSString*)separator{
NSRangerange = [self.textrangeOfString:separator];
if (range.location != NSNotFound)
{
range.length= range.location;
range.location=0;
[selfsetFont:fontrange:range];
}
}
- (void)setFont:(UIFont *)font afterOccurenceOfString:(NSString*)separator{
NSRangerange = [self.textrangeOfString:separator];
if (range.location != NSNotFound)
{
range.length=self.text.length- range.location;
[selfsetFont:fontrange:range];
}
}
- (void)setTextColor:(UIColor*)textColorString:(NSString*)searchString
{
NSRangerange = [self.textrangeOfString:searchString];
if (range.location != NSNotFound)
{
[selfsetTextColor:textColorrange:range];
}
}
- (void)setTextColor:(UIColor*)textColorString:(NSString*)searchStringunderLineColor:(UIColor*)underlineColor
{
NSRangerange = [self.textrangeOfString:searchString];
if (range.location != NSNotFound)
{
[selfsetTextColor:textColorrange:range];
[selfsetTextUnderLine:underlineColorrange:range];
}
}
- (void)setTextUnderLine:(UIColor*)lineColorrange:(NSRange)range
{
NSMutableAttributedString *text = [self.attributedText mutableCopy];
[textaddAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle|NSUnderlineByWord) //[NSNumber numberWithInt:NSSingleUnderlineStyle]
range:range];
[textaddAttribute:NSUnderlineColorAttributeName value:lineColor range:range];
self.attributedText = text;
}
- (void)setTextWithoutUnderLineInRange:(NSRange)range
{
NSMutableAttributedString *text = [self.attributedText mutableCopy];
[textremoveAttribute:NSUnderlineStyleAttributeName
range:range];
self.attributedText = text;
}
- (void)setTextColor:(UIColor *)textColor fromOccurenceOfString:(NSString*)separator1 toOccurenceOfString:(NSString*)separator2
{
NSRangerangeFrom = [self.textrangeOfString:separator1];
NSRange rangeTo = [self.text rangeOfString:separator2 options:0 range:NSMakeRange(rangeFrom.location,self.text.length-rangeFrom.location)];
if(rangeFrom.location!=NSNotFound)
{
if( rangeTo.location==NSNotFound)
rangeFrom.length=self.text.length- rangeFrom.location;
else
rangeFrom.length= rangeTo.location- rangeFrom.location;
[selfsetTextColor:textColorrange:rangeFrom];
if([textColorisEqual:[UIColorclearColor]] ==YES)
{
[selfsetTextUnderLine:[UIColorblueColor]range:rangeFrom];
}
}
}
@end