在UITextField中使用自定义字体会导致其在访问时略微偏移-是否已解决?

如何解决在UITextField中使用自定义字体会导致其在访问时略微偏移-是否已解决?

| 我在UITextField中有一个自定义字体,并且我已经注意到,当它被访问时(出现键盘时),文本向下移动的量非常小-可能是一个像素或两个或三个。 (当然,我没有办法对其进行测量,但是足以让我注意到。)然后,在关闭键盘后,它会再次向上移动。 我已经尝试在编辑时清除字段(这隐藏了初始下移的问题),但尚未解决问题。我也查看了IB中的各种属性,并尝试更改了一些属性,但问题仍然存在。在模拟器和iPad2上,我得到相同的结果。 (该字段距离键盘很远,因此,并不是整个视图移开-而是该特定文本字段的内容。) 我确定这是导致问题的自定义字体-没有它就不会发生。 任何想法如何解决这个问题?我当时想我可能需要以编程方式创建文本字段,而不是在IB中创建文本字段-我知道我可能应该在问这个问题之前先尝试一下,但是我不愿意在可能的情况下解决所有麻烦解决问题。 任何建议表示赞赏!     

解决方法

        我也有这个问题。 要修复此问题,请子集ѭ0并实现以下方法以在不进行编辑和编辑时调整文本的位置。
- (CGRect)textRectForBounds:(CGRect)bounds {

    return CGRectInset( bounds,8,8 );
}

- (CGRect)editingRectForBounds:(CGRect)bounds {

    return CGRectInset( bounds,5 );
}
    ,        我的解决方案与McDJ的解决方案相同,但略有不同。子类化UITextField并仅覆盖以下内容:
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
  return CGRectOffset( [self editingRectForBounds:bounds],2 );
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
  return CGRectOffset( [super editingRectForBounds:bounds],-2 );
}
使用我正在使用的自定义字体,正确的垂直调整是2点,这有助于占位符,“静态”和“编辑”文本都保持在同一垂直线上。     ,        不幸的是,没有答案对我有用。 @blackjacx答案有效,但仅有时:( 我开始调试,这是我发现的内容: 1-真正的问题似乎是在类型为
UIFieldEditorContentView
的UITextField的私有子视图中 在下面您可以看到它的子视图的
y
UITextField
本身不同: 在意识到这一点之后,我想到了以下解决方法:
override func layoutSubviews() {
    super.layoutSubviews()
    fixMisplacedEditorContentView()
}

func fixMisplacedEditorContentView() {
    if #available(iOS 10,*) {
        for view in subviews {
            if view.bounds.origin.y < 0 {
                view.bounds.origin = CGPoint(x: view.bounds.origin.x,y: 0)
            }
        }
    }
}
您将需要子类化“ 0”并覆盖“ 8”,以增加将任何手动设置为负值的子视图中的“ 4”设置为“ 9”的功能。由于iOS 9不会发生此问题,因此我们在下面添加了一项检查,以仅在iOS 10上解决此问题。 您可以在下面看到结果: 2-如果用户选择选择文本的子范围,则此解决方法将不起作用(selectAll可以正常工作) 由于文本的选择对于我的应用程序不是必须的,因此我宁愿禁用它。为此,您可以使用以下代码(Swift 3):
override func canPerformAction(_ action: Selector,withSender sender: Any?) -> Bool {
    if #available(iOS 10,*) {
        if action == #selector(UIResponderStandardEditActions.select(_:)) {
            return false
        }
    }

    return super.canPerformAction(action,withSender: sender)
}
    ,        适用于所有字体大小,不会导致与
clearButton
对齐。 子类“ 0”,并按如下所示重写它们:
- (CGRect)placeholderRectForBounds:(CGRect)bounds
{
    return CGRectOffset( bounds,4 );
}

- (CGRect)editingRectForBounds:(CGRect)bounds
{
    return CGRectOffset( bounds,2);
}

- (CGRect)textRectForBounds:(CGRect)bounds
{
    return CGRectOffset( bounds,4 );
}
    ,        出于一个奇怪的原因,我并没有真正理解我已经通过将autoAdjustsScrollViewInsets设置为NO(或Interface Builder中的等效项)来解决此问题。这是iOS 8.1。     ,        我遇到了一个自定义字体的问题,并通过在键盘事件触发时将标签移到另一个方向来解决此问题。我通过覆盖
drawRect:
方法移动了标签中心
- (void)drawRect:(CGRect)rect
{
    self.titleLabel.center = CGPointMake(self.titleLabel.center.x,self.titleLabel.center.y+3);
}
    ,        这是标准UITextField中的预期行为。但是,您可以通过子类化UITextField并通过调整文本本身的边界来解决此问题。 迅捷3
override func textRect(forBounds bounds: CGRect) -> CGRect {
    return(bounds.insetBy(dx: 0,dy: 0))
}

override func editingRect(forBounds bounds: CGRect) -> CGRect {
    return(bounds.insetBy(dx: 0,dy: -0.5))
}

override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
    return(bounds.insetBy(dx: 0,dy: 0))
}
这应该可以解决问题!     ,        在IB中将文本字段的边框样式设置为\“ none \”以外的任何值,然后在ViewController \的viewDidLoad中设置:
yourTextField.borderStyle = .none
(基于Box Jeon的回答)     ,        迅捷3 不要忘记UITextField的“ 19”。如果需要有效的实现,则需要考虑* rect(forBounds:...)函数的20%。并且还要确保仅将越野车
iOS 10
的整流器移位,而不是将9或8移位。以下代码可以解决问题:
public class CustomTextField: UITextField {
    public override func textRect(forBounds bounds: CGRect) -> CGRect {
        let superValue = super.textRect(forBounds: bounds)

        if #available(iOS 10,*) {
            return superValue.insetBy(dx: 0,dy: 0)
        }
        return superValue
    }

    public override func editingRect(forBounds bounds: CGRect) -> CGRect {
        let superValue = super.editingRect(forBounds: bounds)

        if #available(iOS 10,dy: -0.5)
        }
        return superValue
    }

    public override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        let superValue = super.placeholderRect(forBounds: bounds)

        if #available(iOS 10,*) {
            if isEditing {
                return superValue.insetBy(dx: 0,dy: 0.5)
            }
            return superValue.insetBy(dx: 0,dy: 0.0)
        }
        return superValue
    }
}
编辑 我从上面到下面对代码进行了少量编辑,对我来说效果更好。我在iPhone 6、6s,7、7s以及带有iOS 9.3和10.3的'plus'设备上进行测试。
public class CustomTextField: UITextField {

    public override func textRect(forBounds bounds: CGRect) -> CGRect {
        let superValue = super.textRect(forBounds: bounds)

        if #available(iOS 10,dy: -0.3)
        }
        return superValue.insetBy(dx: 0,dy: -0.2)
    }

    public override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return self.textRect(forBounds: bounds)
    }
}
我认为这也取决于您使用的字体。我用
UIFont.systemFont(ofSize: 17.0,weight: UIFontWeightLight)
    ,        您应将Font属性设置为早于Text属性。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res