2012年11月11日 星期日

Dynamic UILabel

- (void)loadView
{
    [super loadView];
    //1.UILable的大小自适应实例:
    UILabel *myLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 2, 2)];//设定位置与大小
    [myLabel setFont:[UIFont fontWithName:@"Helvetica" size:20.0]];//格式
    [myLabel setNumberOfLines:0];//行数,只有设为0才可以自适应
    [myLabel setBackgroundColor:[UIColor clearColor]];//背景色
    myLabel.shadowColor = [UIColor darkGrayColor];//阴影颜色
    myLabel.shadowOffset = CGSizeMake(1.0,1.0);//阴影大小
    
    NSString *text = @"abcdefghigklmnopqrstuvwxyz";
    UIFont *font = [UIFont fontWithName:@"Helvetica" size:20.0];
    CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f) lineBreakMode:UILineBreakModeWordWrap];
    CGRect rect=myLabel.frame;
    rect.size=size;
    [myLabel setFrame:rect];
    [myLabel setText:text];
     myLabel.shadowColor = [UIColor darkGrayColor];//阴影颜色
     myLabel.shadowOffset = CGSizeMake(2.0,2.0);//阴影大小
    [self.view addSubview:myLabel];
    [myLabel release];
    //2.UILable的基本用法获取自馒头MAN百度空间,感谢馒头MAN
            //空间地址:http://hi.baidu.com/bunsman/blog/item/95777b0ebacf05fe36d122e2.html
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 40.0, 200.0, 30.0)];     
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 80.0, 200.0, 50.0)];     
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];     
    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];     
    UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];     
    UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];     
    UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];     
    
    //设置显示文字     
    label1.text = @"label1";     
    label2.text = @"label2";     
    label3.text = @"label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--11个";     
    label4.text = @"label4--label4--label4--label4--4个";     
    label5.text = @"label5--label5--label5--label5--label5--label5--6个";     
    label6.text = @"label6";     
    label7.text = @"label7";     
    
    //设置字体:粗体,正常的是 SystemFontOfSize     
    label1.font = [UIFont boldSystemFontOfSize:20];     
    
    //设置文字颜色  
    label1.textColor = [UIColor orangeColor];     
    label2.textColor = [UIColor purpleColor];     
    //设置背景颜色 
    label1.backgroundColor = [UIColor clearColor];
    label2.backgroundColor = [UIColor  colorWithRed:0.5f green:30/255.0f blue:0.3f alpha:0.5f];
    //设置文字位置     
    label1.textAlignment = UITextAlignmentRight;     
    label2.textAlignment = UITextAlignmentCenter;     
    //设置字体大小适应label宽度     
    label4.adjustsFontSizeToFitWidth = YES;
    //设置label的行数     
    label5.numberOfLines = 2;     
    
    //设置高亮     
    label6.highlighted = YES;     
    label6.highlightedTextColor = [UIColor orangeColor];     
    
    //设置阴影     
    label7.shadowColor = [UIColor redColor];     
    label7.shadowOffset = CGSizeMake(1.0,1.0);     
    
    //设置是否能与用户进行交互     
    
    label7.userInteractionEnabled = YES;     
    
    //设置label中的文字是否可变,默认值是YES     
    label3.enabled = NO;     
    
    //设置文字过长时的显示格式     
    
    label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间     
    //  typedef enum {     
    //      UILineBreakModeWordWrap = 0,     
    //      UILineBreakModeCharacterWrap,     
    //      UILineBreakModeClip,//截去多余部分     
    //      UILineBreakModeHeadTruncation,//截去头部     
    //      UILineBreakModeTailTruncation,//截去尾部     
    //      UILineBreakModeMiddleTruncation,//截去中间     
    //  } UILineBreakMode;     
    
    //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为     
    label4.baselineAdjustment = UIBaselineAdjustmentNone;     
    //  typedef enum {     
    //      UIBaselineAdjustmentAlignBaselines,     
    //      UIBaselineAdjustmentAlignCenters,     
    //      UIBaselineAdjustmentNone,     
    //  } UIBaselineAdjustment;     
    
    
    [self.view addSubview:label1];     
    [self.view addSubview:label2];     
    [self.view addSubview:label3];     
    [self.view addSubview:label4];     
    [self.view addSubview:label5];     
    [self.view addSubview:label6];     
    [self.view addSubview:label7];     
    
    [label1 release];     
    [label2 release];     
    [label3 release];     
    [label4 release];     
    [label5 release];     
    [label6 release];     
    [label7 release];         
    
}

2012年10月20日 星期六

CICS TS 4.2 for z/OS Messages & Codes iPhone version

Find this APP "CICS TS messages and Codes"

2012年9月29日 星期六

Create a Quick Response Code (QR Code) image using Google Chart

Here is an example of generating a QR code image for the My developerWorks iphone app on iTunes: "<"img src="https://chart.googleapis.com/chart?chs=250x250&cht=qr&chl=http://itunes.apple.com/us/app/developerworks/id391162970?mt=8&choe=UTF-8" alt="Download developerWorks app" /">" Download developerWorks app QR codes have a different set of required parameters from any other chart:

2012年9月7日 星期五

iPhone Socket Programing in Objective-C

“What is Socket programming.”? So it is simply a way to communicate with two machines using UDP and TCP/IP protocol. Socket are interfaces that allows you to transfer data in bidirectional way means from one app to another app. Socket has two sides. Each side of it is identified by a combination of IP address and port. Socket programing is used in various situation in iOS for iPad, iPod and iPhone application development like on line gaming, chat application etc. There are lots of guides and articles available to make socket connection between apps but these are very limited in Objective-C and it is very complicated to integrate them. This article discusses only how we can create a socket connection from one app to another app. After establishing the connection both apps can send and receive data to and from each other. Socket Programming in Objective-C can be described in 3 parts: 1. Setting Socket Connection 2. Reading from Socket Connection 3. Writing to Socket Connection For detailed implementation of these 3 parts of Socket Programming refer to: Socket_Programing_for_IOS.pdf

2012年8月28日 星期二

2012年8月2日 星期四

iPhone socket sample

client.... #import #include #include #include #include #include #import void dealWithData(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) { /* printf("received %d bytes from socket %d\n", CFDataGetLength((CFDataRef)data), CFSocketGetNative(s)); */ UInt8 buffer[100]; CFDataGetBytes((CFDataRef)data, CFRangeMake(0,CFDataGetLength((CFDataRef)data)),buffer); printf("%s\n",(char*)buffer); } main () { CFSocketRef s = CFSocketCreate(NULL, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketDataCallBack, dealWithData, NULL); CFDataRef address, data; struct sockaddr_in sin; char message[] = "Insert message to send here.\n"; CFRunLoopSourceRef source; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(8880); address = CFDataCreate(NULL, (char *)&sin, sizeof(sin)); data = CFDataCreate(NULL, message, sizeof(message)); CFSocketConnectToAddress(s, address, 0); CFSocketSendData(s, NULL, data, 0); CFRelease(address); CFRelease(data); source = CFSocketCreateRunLoopSource(NULL, s, 0); CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode); CFRelease(source); CFRelease(s); CFRunLoopRun(); }

2012年6月8日 星期五

The function to replace string in C

#include 
#include 

char * replace(
    char const * const original, 
    char const * const pattern, 
    char const * const replacement
) {
  size_t const replen = strlen(replacement);
  size_t const patlen = strlen(pattern);
  size_t const orilen = strlen(original);

  size_t patcnt = 0;
  const char * oriptr;
  const char * patloc;

  // find how many times the pattern occurs in the original string
  for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
  {
    patcnt++;
  }

  {
    // allocate memory for the new string
    size_t const retlen = orilen + patcnt * (replen - patlen);
    char * const returned = (char *) malloc( sizeof(char) * (retlen + 1) );

    if (returned != NULL)
    {
      // copy the original string, 
      // replacing all the instances of the pattern
      char * retptr = returned;
      for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
      {
        size_t const skplen = patloc - oriptr;
        // copy the section until the occurence of the pattern
        strncpy(retptr, oriptr, skplen);
        retptr += skplen;
        // copy the replacement 
        strncpy(retptr, replacement, replen);
        retptr += replen;
      }
      // copy the rest of the string.
      strcpy(retptr, oriptr);
    }
    return returned;
  }
}

#include 
int main(int argc, char * argv[])
{
  if (argc != 4)
  {
    fprintf(stderr,"usage: %s   \n", argv[0]);
    exit(-1);
  }
  else
  {
    char * const newstr = replace(argv[1], argv[2], argv[3]);
    if (newstr)
    {
      printf("%s\n", newstr);
      free(newstr);
    }
    else
    {
      fprintf(stderr,"allocation error\n");
      exit(-2);
    }
  }
  return 0;
}