local-host 2010-09-16 13:21 采纳率: 100%
浏览 708
已采纳

使用 UITextBorderStyleNone 为 UITextField 设置填充

I wanted to use a custom background for my UITextFields. This works fine except for the fact that I have to use UITextBorderStyleNone to make it look pretty. This forces the text to stick to the left without any padding.

Can I set a padding manually so that it looks similar to UITextBorderStyleRoundedRect except for using my custom background image?

转载于:https://stackoverflow.com/questions/3727068/set-padding-for-uitextfield-with-uitextborderstylenone

  • 写回答

30条回答 默认 最新

  • csdnceshi62 2010-12-12 20:20
    关注

    I found a neat little hack to set the left padding for this exact situation.

    Basically, you set the leftView property of the UITextField to be an empty view of the size of the padding you want:

    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
    textField.leftView = paddingView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    

    Worked like a charm for me!

    In Swift 3, it can be done by doing that

    let paddingView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 20))
    textField.leftView = paddingView
    textField.leftViewMode = .always
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(29条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序