美文网首页
iOS button 图片居右文字居左

iOS button 图片居右文字居左

作者: 小的小碰撞 | 来源:发表于2017-05-18 11:29 被阅读0次

    button 图片居右文字居左

    • 默认button中的图片是左文字是右 最终想要的是这样
    [self.phoneLoginBtn setTitleEdgeInsets:UIEdgeInsetsMake(0,-self.phoneLoginBtn.imageView.size.width, 0, self.phoneLoginBtn.imageView.size.width)];
    
     [self.phoneLoginBtn setImageEdgeInsets:UIEdgeInsetsMake(0, self.phoneLoginBtn.titleLabel.bounds.size.width, 0, -self.phoneLoginBtn.titleLabel.bounds.size.width)];
    
    05.png
    • 原始图片是这样的:
    01.png

    · 当如下设置时:效果是这样的

    • 02 向左40 等价于向右-40
    // 02 向左40
        [self.myButton setImageEdgeInsets:UIEdgeInsetsMake(0, 40, 0, 0)];
    // 向右-40
         [self.myButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -40)];
    // 
       [self.myButton setImageEdgeInsets:UIEdgeInsetsMake(0, 20, 0, -20)];
    
    ![Uploading 03_421791.png . . .]
    • 03 向右40 等价于向左-40
    // 03 向右40
     [self.myButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 40)];
    
     [self.myButton setImageEdgeInsets:UIEdgeInsetsMake(0, -40, 0, 0)];
    [self.myButton setImageEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 20)];
    
    03.png 04.png
    • 也就是最终我们希望图片上下位置不变top = 0, bottom = 0;向右位移一个label的宽度

    同时标题上下位置不变top = 0, bottom = 0;向左位移一个图片的宽度

    即 imageEdgeInsets = UIEdgeInsetsMake(0,0 + labelWidth,0,0 - labelWidth); 图片相对初始状态左边距加了labelWidth,右边减labelWidth,以及titleEdgeInsets = UIEdgeInsetsMake(0,0 - imageViewWidth,0, 0 + imageViewWidth);

    2. button自定义 重写layoutSubviews方法

    - (void)layoutSubviews
    {
        [super layoutSubviews];
        
        // 调整图片
        self.imageView.x = 0;
        self.imageView.y = 0;
        self.imageView.width = self.width;
        self.imageView.height = self.imageView.width;
        
        // 调整文字
        self.titleLabel.x = 0;
        self.titleLabel.y = self.imageView.height;
        self.titleLabel.width = self.width;
        self.titleLabel.height = self.height - self.titleLabel.y;
    }
    
    

    stringByAppendingPathComponent --后面默认拼接路径 /

        NSString *str = @"hehe";
        
        NSString *new = [str stringByAppendingPathComponent:@"nicai"];
        // log --->  new----hehe/nicai
        NSString *new2 = [str stringByAppendingString:@"nicai"];
        // log ---> new----hehenicai
    

    IOS MJExtension 关键字替换

    [XMGVideosetupReplacedKeyFromPropertyName:^NSDictionary*{
    
    return@{@"ID":@"id"};
    
    }];
    

    相关文章

      网友评论

          本文标题:iOS button 图片居右文字居左

          本文链接:https://www.haomeiwen.com/subject/uwhsxxtx.html