textfield - 动态文本大小调整以适应整个文本行到文本字段

标签 textfield livecode text-formatting

我有大约 640 行不同长度的文本行,它们既可以作为数据网格中的列表查看,也可以在单行 View 中查看。在单行 View 中,我想调整文本行的字体大小,以便它

  1. 完全适合文本字段

  2. 最大磅值 24 和最小磅值 10(我已在文本字段中为其设置了自定义属性)。

我在单行 View 卡片中使用了以下代码:

on resizeText

 ## Check if the text fits in the field
 put the textSize of fld "foneline" into tTextSize
 if the formattedHeight of fld "foneline" > the height of fld "foneline" then

 ## Make the text smaller until it fits
 repeat until the formattedHeight of fld "foneline" <= the height of fld "foneline" 
   subtract 1 from tTextSize

  ## Check that the text size is not less that the minimum size       
  if tTextSize >= the cMinimumTextSize of fld "foneline" then     
       set the textSize of fld "foneline" to tTextSize      
  else         
   exit repeat       
  end if    
  end repeat  
 else if the formattedHeight of fld "foneline" < the height of fld "foneline" then

  ## Make the text as large as possible    
 repeat until the formattedHeight of fld "foneline" >= the height of fld "foneline"      
   add 1 to tTextSize         

  ## Check that the text size is not bigger that the maximum size      
   if tTextSize<= the cMaximumTextSize of fld "foneline" then       
     set the textSize of fld "foneline" to tTextSize      
  else        
    exit repeat       
  end if     
 end repeat  
 end if
end resizeText

以及卡片上 Previous 和 Next 按钮代码中的 resizeText 命令。

然而,在显示的代码中存在一些小故障或错误,我希望您可以按照以下步骤进行复制:

  1. 打开堆栈

  2. 点击“全选”按钮

  3. 点击“线条”按钮

  4. 单击“下一步”按钮,直到您看到第 6 行(它的全长可见)。

  5. 再次单击“下一步”按钮,您将看到第 7 行,但该行并不完全可见(您会看到右引号 "不存在)

  6. 点击“Next”按钮,然后点击“Previous”按钮 - 这会将您带回到第 7 行,现在可以看到它的全长!

  7. 现在点击“上一步”按钮,第 6 行可见,但这次不是全长

  8. 点击“Previous”,然后再次点击“Next”,现在第 6 行已完整显示

  9. 点击“下一步”,第 7 行再次完全不可见。

如果您无法使用上面提到的行号复制它,请尝试继续单击“下一步”按钮,直到您遇到缺少结尾 "引号的行,这表明该行已被截断。然后按照与上面相同的步骤单击“上一个”和“下一个”按钮。这听起来可能很复杂,但是一旦你打开堆栈,就很容易找出错误的行为。类似的情况是行:83-84-85 ;48-49;51-52。 我的堆栈的链接是 DG-only-1.16_cut-off-lines.zip

如何更正代码,使其始终显示每一行而不切断行尾?

最佳答案

在您的“resizeText”处理程序中:如果格式化的高度小于该字段,那么您将调整文本的大小直到它>= formattedHeight。这意味着循环有时会在其高度大于所需高度时停止。通过在字体大小增加时添加最终高度检查,我们可以避免这种情况;

on resizeText
   ## Check if the text fits in the field
   put the textSize of fld "foneline" into tTextSize
   put the height of fld "foneline"  into tHeight

   if the formattedHeight of fld "foneline" > tHeight then
      ## Make the text smaller until it fits
      repeat until the formattedHeight of fld "foneline" <= tHeight 
         subtract 1 from tTextSize

         ## Check that the text size is not less that the minimum size
         if tTextSize >= the cMinimumTextSize of fld "foneline" then
            set the textSize of fld "foneline" to tTextSize
         else
            exit repeat
         end if
      end repeat      
   else if the formattedHeight of fld "foneline" < tHeight then
      ## Make the text as large as possible
      repeat until the formattedHeight of fld "foneline" >= tHeight
         add 1 to tTextSize

         ## Check that the text size is not bigger that the maximum size
         if tTextSize<= the cMaximumTextSize of fld "foneline" then
            set the textSize of fld "foneline" to tTextSize
         else
            exit repeat
         end if
      end repeat

      # check the final field height
      if the formattedHeight of fld "foneline" > tHeight then
         subtract 1 from tTextSize
         set the textSize of fld "foneline" to tTextSize
      end if
   end if
end resizeText

关于textfield - 动态文本大小调整以适应整个文本行到文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24794763/

相关文章:

ios - 如何创建只接受数字和一个点的 SwiftUI TextField?

javascript - 如果文本字段值匹配,则单击时更改按钮图像源

ios - 如何在 LiveCode 中使用 mergMK 创建可点击的 POI?

sed:从文件中删除字母数字单词

java - Java中使用一个TextField输入多个数据

ios - AS3/Air 将焦点从一个文本字段转移到 ios 中的下一个文本字段

audio - 在 Livecode 中播放多个声音文件

javascript - push 和 length 数组操作,遍历数组

html - 在 HTML 中显示段落

java - 使用扫描仪的分隔符功能来查找 "abc-def"