ios novice development --Label dispersion aligned with height measurement UIView

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_16674697/article/details/53172388

Recently the company in order to achieve a dynamic publishing mode (Figure :), can be described as the IOS dynamic layout to the extreme, especially the use of the label. Ios development came into contact with when that label does not like Andrews so smart, because ios position the controls are absolutely, without the use of any third party or autolayout layout is very easy to do show some different heights adaptive control. this blogger in the "first Baidu, after making the wheel" in attitude, their encapsulates some common methods of obtaining highly adaptive view:


First state several macro definition, we reduce the amount of code to facilitatelaughing out loud

#define VH(X) X.frame.size.height
#define VW(X) X.frame.size.width
#define VX(X) X.frame.origin.x
#define VY(X) X.frame.origin.y
#define VF(v,x,y,w,h) v.frame=CGRectMake(x, y, w, h);

UIlabel achieve highly adaptive

/*
 * label高度自适应,调用前需设置文本和字体大小
 */
+(CGFloat)getlabelHeightWithLabel:(UILabel*)lab{
    [lab sizeToFit];
    lab.numberOfLines = 0;
    lab.lineBreakMode = NSLineBreakByWordWrapping;
    CGSize size = [lab sizeThatFits:CGSizeMake(VW(lab), MAXFLOAT)];
    if([lab.text isEqualToString:@""]){
        size.height=VH(lab);
    }
    return size.height;
}

The method is simple, pass a good target has been set label text, and then set some wrap attribute to invoke sizeThatFits method returns highly adaptive, attention must set the font and text label before using this method -

Distribute UIlabel line text:


/*
 * 文本分散对齐
 */
+(NSMutableAttributedString*)fillWords:(NSString*)str andWidth:(CGFloat)width andFont:(UIFont*)font{
    NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:str];
    if(str.length==0||str.length==1){
        return attrStr;
    }
    UILabel * lab=[[UILabel alloc]init];
    lab.font=font;
    lab.attributedText=attrStr;
    [lab sizeToFit];
    CGFloat offsetWidth;
    offsetWidth=(width-VW(lab))/(str.length-1);
    [attrStr addAttribute:NSKernAttributeName value:@(offsetWidth) range:NSMakeRange(0, attrStr.length-1)];
    return attrStr;
}

There are three parameters, namely: text, font default width and aligned to achieve single-line dispersion, the principle is very simple, we give a default label display width, then the adaptive measured width of the text, then the total width / text width. the spacing can be obtained between each word, the word spacing is provided by a dispersion in order to achieve alignment.

After UIView highly adaptive applet to display


/*
 * 获取view的子view高度和
 */
+(CGFloat)getHeightOfView:(UIView *)view{
    CGFloat height=VY(view)+VH(view);
    if([view subviews].count!=0){
        UIView * childView=[view subviews][[view subviews].count-1];
        height=VY(childView)+VH(childView);
        if([view subviews].count>=2){
            CGFloat cHeight;
            UIView * secondView=[view subviews][[view subviews].count-2];
            cHeight=VY(secondView)+VH(secondView);
            if(cHeight>height){
                return cHeight;
            }
        }
    }
    return height;
}

The first method is to obtain a view of the last sub-View, and then by comparing the last sub-View of the height and the penultimate sub-View, to return the maximum height. This realizes an adaptive calculation uiview height.


to sum up

With these three methods, and then to achieve the above said, the effect is very simple, just use a for loop can be easily solved, this is not pasted code. As for the color red mark many ways, this will not go into details. in fact, IOS dynamic layout with respect to the development of Android, it is not an advantage, but can also be compensated by information on all aspects, in this case, the application of the IOS label some more in-depth. ios unwittingly have done almost forty months, has been described for Android into the harem, the end of this stage, the body will be to focus on Android and look forward to my next imitation iosQQ drop-down refresh readers, please add their own concerns yo ~laughing out loud


Author: yangpeixing

QQ:313930500

Please indicate the source ~ Thank you ~




Guess you like

Origin blog.csdn.net/qq_16674697/article/details/53172388