zoukankan      html  css  js  c++  java
  • IOStextField或textView赋予内容string

    赋予内容

    视图中的文字可以通过一个名为text的属性来设置。这个属性接受一个NSString作为参数。下面给出了一种设置静态文本的简单方法:

    1. textView.text = @"Hello, world!"; 

    你也可以用NSString类中的多种字符串创建方法,来定制创建字符串对象:

    1. int nBottles = 100;  
    2. NSString *myFormattedString = [ [ NSString alloc ]  
    3.         initWithFormat: @"%d bottles of beer on the wall", nBottles  
    4. ];  
    5.  
    6. textView.text = myFormattedString

    你还可以从C语言风格的字符数组中创建NSString对象:

    1. char myBottles[] = "100 bottles of beer on the wall";  
    2. NSString *myCString = [ NSString stringWithCString: myBottles ];  
    3. textView.text = myCString

    如果你要访问主目录下的文件,可以用NSHomeDirectory函数得到你的应用程序沙箱的唯一路径。这个路径指向你在第1章中学到的目录结构:

    1. NSString *myFile = [ NSHomeDirectory()  
    2.         stringByAppendingPathComponent: @"Documents/file.txt"  
    3. ];  
    4.  
    5. NSString *myFileString = [ NSString stringWithContentsOfFile: myFile ];  
    6. textView.text = myFileString

    还有一个同样的实例方法,不过是用initWith前缀,而不是stringWith:

      1. NSString *myFile = [ [ NSString alloc ] initWithFormat: 
        @"%@/Documents/ file.txt",  
      2.         NSHomeDirectory()  
      3. ];  
      4.  
      5. NSString *myFileString = [ [ NSString alloc ] 
        initWithContentsOfFile: myFile ];  
      6. textView.text = myFileString;
  • 相关阅读:
    36、【opencv入门】运动物体检测(2)
    二叉树数
    多边形的三角划分
    乘积最大
    加分二叉树
    c++ 装箱问题
    生物基元问题
    一般性的最少硬币组成问题
    打包
    挤牛奶
  • 原文地址:https://www.cnblogs.com/tx8899/p/2536852.html
Copyright © 2011-2022 走看看