Skip to content

Commit

Permalink
批量增加以下属性:
Browse files Browse the repository at this point in the history
UIView::font,格式为   字体名称|字体大小    字体名称也可以是bold或者italic
FlexTextView::placeholder
FlexTextView::placeholderColor
UILabel::lineSpacing
UILabel::paragraphSpacing
UILabel::firstLineHeadIndent
UILabel::headIndent
UILabel::tailIndent
  • Loading branch information
zhenglibao committed Jun 2, 2018
1 parent b2b1af7 commit ddc3c11
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 4 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
**FlexLib**的所有版本的变更日志都将会在这里记录.

---
## 1.8.2
批量增加以下属性:
UIView::font,格式为 字体名称|字体大小 字体名称也可以是bold或者italic
FlexTextView::placeholder
FlexTextView::placeholderColor
UILabel::lineSpacing
UILabel::paragraphSpacing
UILabel::firstLineHeadIndent
UILabel::headIndent
UILabel::tailIndent


## 1.8.1
1. FlexFrameView增加对safeArea的支持,能够更好的用在ViewController上(也就是FlexFrameView可以不再和子FlexRootView的大小保持一致),使用时直接设置FlexRootView的safeArea属性即可。
2. 修复FlexContainerView在所有子view均隐藏时返回大小不正确的问题
Expand Down
44 changes: 44 additions & 0 deletions Doc/res/vscode-xml.json
Original file line number Diff line number Diff line change
Expand Up @@ -1945,4 +1945,48 @@
"continue",
],
},

// added at 2018/6/2
"lineSpacing":{
"prefix": "lineSpacing",
"body": [
"lineSpacing",
],
},
"paragraphSpacing":{
"prefix": "paragraphSpacing",
"body": [
"paragraphSpacing",
],
},
"firstLineHeadIndent":{
"prefix": "firstLineHeadIndent",
"body": [
"firstLineHeadIndent",
],
},
"headIndent":{
"prefix": "headIndent",
"body": [
"headIndent",
],
},
"tailIndent":{
"prefix": "tailIndent",
"body": [
"tailIndent",
],
},
"font":{
"prefix": "font",
"body": [
"font",
],
},
"placeholderColor":{
"prefix": "placeholderColor",
"body": [
"placeholderColor",
],
},
}
2 changes: 1 addition & 1 deletion FlexLib.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'FlexLib'
s.version = '1.8.1'
s.version = '1.8.2'
s.summary = 'An obj-c flex layout framework for IOS'

# This description is used to generate tags and improve search results.
Expand Down
6 changes: 6 additions & 0 deletions FlexLib/Classes/FlexTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@
//
@interface FlexTextView : UITextView

@property(nonatomic,readonly) UILabel *placeholdLabel;
@property(nonatomic,strong) NSString *placeholder;
@property(nonatomic,strong) UIColor *placeholderColor;
@property(nonatomic,nonnull,strong) NSAttributedString *attributePlaceholder;
@property(nonatomic,assign) CGPoint location;

@end
136 changes: 134 additions & 2 deletions FlexLib/Classes/FlexTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,46 @@

#import "FlexTextView.h"
#import "FlexRootView.h"
#import "FlexUtils.h"
#import "YogaKit/UIView+Yoga.h"


@interface FlexTextView()
{
UILabel* _placeholderLabel;
}
@property(nonatomic,assign) BOOL needAdjustFont;
@end

@implementation FlexTextView

- (instancetype)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];

UILabel* label = [[UILabel alloc] init];
label.textAlignment = NSTextAlignmentLeft;
label.numberOfLines = 0;
label.textColor = [self.class defaultColor];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:nil];

[self addObserver:self forKeyPath:@"font" options:NSKeyValueObservingOptionNew context:nil];

_placeholderLabel = label;
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)textDidChange:(NSNotification *)notification
{
[self updatePlaceholder];

CGRect rcSelf = self.frame;
CGSize szLimit = CGSizeMake(rcSelf.size.width, FLT_MAX);
CGSize sz = [self sizeThatFits:szLimit];
Expand All @@ -50,4 +71,115 @@ - (void)textDidChange:(NSNotification *)notification
}
}
}

#pragma mark - placeholder

- (UILabel *)placeholdLabel
{
return _placeholderLabel;
}

+ (UIColor *)defaultColor
{
static UIColor *color = nil;
static dispatch_once_t once_t;
dispatch_once(&once_t, ^{
UITextField *textField = [[UITextField alloc] init];
textField.placeholder = @" ";
color = [textField valueForKeyPath:@"_placeholderLabel.textColor"];
});
return color;
}

#pragma mark - set get methods

- (void)setPlaceholder:(NSString *)placeholder
{
_placeholderLabel.text = placeholder;
[self updatePlaceholder];
}

- (NSString *)placeholder
{
return _placeholderLabel.text;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
_placeholderLabel.textColor = placeholderColor;
[self updatePlaceholder];
}

- (UIColor *)placeholderColor
{
return self.placeholdLabel.textColor;
}

- (void)setAttributePlaceholder:(NSAttributedString *)attributePlaceholder
{
_placeholderLabel.attributedText = attributePlaceholder;
[self updatePlaceholder];
}

- (NSAttributedString *)attributePlaceholder
{
return _placeholderLabel.attributedText;
}


- (void)updatePlaceholder
{
if (self.text.length) {
[_placeholderLabel removeFromSuperview];
return;
}

[self insertSubview:_placeholderLabel atIndex:0];

if (self.needAdjustFont) {
self.placeholdLabel.font = self.font;
self.needAdjustFont = NO;
}

CGFloat lineFragmentPadding = self.textContainer.lineFragmentPadding;
UIEdgeInsets contentInset = self.textContainerInset;

CGFloat labelX = lineFragmentPadding + contentInset.left;
CGFloat labelY = contentInset.top;
if (self.location.x != 0 || self.location.y != 0) {
if (self.location.x < 0 || self.location.x > CGRectGetWidth(self.bounds) - lineFragmentPadding - contentInset.right || self.location.y< 0) {
}else{
labelX += self.location.x;
labelY += self.location.y;
}
}
CGFloat labelW = CGRectGetWidth(self.bounds) - contentInset.right - labelX;
CGFloat labelH = [self.placeholdLabel sizeThatFits:CGSizeMake(labelW, MAXFLOAT)].height;
_placeholderLabel.frame = CGRectMake(labelX, labelY, labelW, labelH);
}


#pragma mark - observer font KVO

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
if ([keyPath isEqualToString:@"font"])
{
self.needAdjustFont = YES;
[self updatePlaceholder];
}
}

#pragma mark - export attr

FLEXSET(placeholder)
{
self.placeholder = sValue ;
}
FLEXSET(placeholderColor)
{
UIColor* clr = colorFromString(sValue,owner) ;
if(clr)
self.placeholderColor = clr ;
}
@end
3 changes: 3 additions & 0 deletions FlexLib/Classes/FlexUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ UIColor* colorFromString(NSString* clr,NSObject* owner);
// eg: white/black/....
UIColor* systemColor(NSString* clr);

// 字符串转字体,格式: 字体名称|字体大小 其中字体名称也可以是bold或者italic
UIFont* fontFromString(NSString* fontStr);

// 字符串转换BOOL
BOOL String2BOOL(NSString* s);

Expand Down
23 changes: 23 additions & 0 deletions FlexLib/Classes/FlexUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@
alpha:a / 255.0f];
}

UIFont* fontFromString(NSString* fontStr)
{
NSArray* ary = [fontStr componentsSeparatedByString:@"|"];

if(ary.count==0)
return nil;

if(ary.count==1){
return [UIFont systemFontOfSize:[ary.firstObject floatValue]];
}

NSString* fontName = ary.firstObject;
CGFloat fontSize = [ary[1] floatValue];

if([@"bold" compare:fontName]==NSOrderedSame)
return [UIFont boldSystemFontOfSize:fontSize];

if([@"italic" compare:fontName]==NSOrderedSame)
return [UIFont italicSystemFontOfSize:fontSize];

return [UIFont fontWithName:fontName size:fontSize];;
}


int String2Int(const char* s,
NameValue table[],
Expand Down
69 changes: 68 additions & 1 deletion FlexLib/Classes/ViewExt/UILabel+Flex.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#import <objc/runtime.h>
#import "../FlexUtils.h"

static char *PARASTYLEKEY = "paraStyleKey";

static NameValue _align[] =
{
{"left", NSTextAlignmentLeft},
Expand All @@ -34,7 +36,11 @@

@implementation UILabel (Flex)

FLEXSETSTR(text)
FLEXSET(text)
{
self.text = sValue;
[self updateAttributeText];
}
FLEXSET(fontSize)
{
float nSize = [sValue floatValue];
Expand Down Expand Up @@ -91,4 +97,65 @@ @implementation UILabel (Flex)
{
self.text = sValue;
}

-(NSMutableParagraphStyle*)paraStyle
{
NSMutableParagraphStyle *style = objc_getAssociatedObject(self, PARASTYLEKEY);
if (!style) {
style = [[NSMutableParagraphStyle alloc] init];

objc_setAssociatedObject(self, PARASTYLEKEY, style, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return style;
}

FLEXSET(lineSpacing)
{
NSMutableParagraphStyle* style=[self paraStyle];
style.lineSpacing = [sValue floatValue];
if(self.text){
[self updateAttributeText];
}
}
FLEXSET(paragraphSpacing)
{
NSMutableParagraphStyle* style=[self paraStyle];
style.paragraphSpacing = [sValue floatValue];
if(self.text){
[self updateAttributeText];
}
}
FLEXSET(firstLineHeadIndent)
{
NSMutableParagraphStyle* style=[self paraStyle];
style.firstLineHeadIndent = [sValue floatValue];
if(self.text){
[self updateAttributeText];
}
}
FLEXSET(headIndent)
{
NSMutableParagraphStyle* style=[self paraStyle];
style.headIndent = [sValue floatValue];
if(self.text){
[self updateAttributeText];
}
}
FLEXSET(tailIndent)
{
NSMutableParagraphStyle* style=[self paraStyle];
style.tailIndent = [sValue floatValue];
if(self.text){
[self updateAttributeText];
}
}
-(void)updateAttributeText
{
NSString* text = self.text ;

NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:self.text];
NSMutableParagraphStyle * style = [self paraStyle];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0,text.length)];
[self setAttributedText:attributedString1];
}
@end
Loading

0 comments on commit ddc3c11

Please sign in to comment.